Skip to content

[BUG] Cannot deduce a braced-init-list (perhaps "parens-init-list" in Cpp2-speak) #1020

Closed
@bluetarpmedia

Description

@bluetarpmedia

Describe the bug
Cannot deduce a C++ braced-init-list (written with parens in Cpp2). Instead I have to explicitly write out the type.

To Reproduce
I'm trying to translate this C++ code:

#include <initializer_list>
#include <iostream>

int main()
{
    const auto ints = {11, 22, 33, 44, 55};
    for (auto i : ints) {
        std::cout << i << " ";
    }
}

into Cpp2:

main: () -> int = {
    ints: const _ = (11, 22, 33, 44, 55);
    
    for ints do (i) {
        std::cout << "(i)$ ";
    }
}

But the relevant line lowers to:

auto const ints {11, 22, 33, 44, 55};

And the C++ compiler reports this error:

error: initializer for variable 'ints' with type 'const auto' contains multiple expressions
    2 |     auto const ints {11, 22, 33, 44, 55}; 
      |     ~~~~~~~~~~~~~~~      ^

Repro on Godbolt

Changing the declaration to the following works as desired:

ints: const std::initializer_list<int> = (11, 22, 33, 44, 55);

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions