Skip to content

[BUG] Cannot initialize std::map with initializer list #1037

@bluetarpmedia

Description

@bluetarpmedia

Describe the bug
I'd like to initialize a std::map (and other associative containers) with an initializer list or some other terse syntax similar to C++.

To Reproduce
I'm trying to translate the following C++ code into Cpp2:

std::map<std::string, int> m{{"CPU", 10}, {"GPU", 15}, {"RAM", 20}};

from the std::map example on cppreference.

This attempt fails:

m: std::map<std::string, int> = (("CPU", 10), ("GPU", 15), ("RAM", 20));

because it lowers to:

std::map<std::string,int> m {("CPU", 10), ("GPU", 15), ("RAM", 20)};

Both of the following succeed:

m: std::map<std::string, int> = (
        std::make_pair("CPU", 10),
        std::make_pair("GPU", 15),
        std::make_pair("RAM", 20));
m: std::map<std::string, int> = (
        :std::pair = ("CPU", 10),
        :std::pair = ("GPU", 15),
        :std::pair = ("RAM", 20));

But they don't have the same terse syntax as the original C++.

See also:
#568 - [BUG] Multidimensional std::array initialization

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