0

I'm trying to initialize a map with the brackets in c++, like this:

    const unordered_map <int, int> prova = 
    {
        {0,1},
        {4,2}
    };

If I compile on VSCode I keep getting the error: non-aggregate type 'const unordered_map<int, int>' cannot be initialized with an initializer list.

I included -std=c++20 in the compiler arguments configuration field and tried every combination of g++/clang/clang++/gcc etc. getting the same result.

When I compile from the terminal instead, with g++ -std=c++20 -o prova prova.cpp everything works fine unless I omit the argument -std=c++20, in which case I get the same error as above.

I have to admit my knowledge about c++ is very limited. As far as I know, my system is up to date. Full output of g++ —-version is the following

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1 Apple clang version 13.0.0 (clang-1300.0.29.30) Target: x86_64-apple-darwin20.6.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

The .vscode/settings.json file is empty, while .vscode/c_cpp_properties.json looks like this:

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/g++",
            "cStandard": "c17",
            "cppStandard": "c++20",
            "intelliSenseMode": "macos-clang-x64",
            "compilerArgs": [
                "-std=c++20"
            ]
        }
    ],
    "version": 4
}

and this is .vscode/tasks.json:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: clang++ compila il file attivo",
            "command": "/usr/bin/clang++",
            "args": [
                "-fcolor-diagnostics",
                "-fansi-escape-codes",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Attività generata dal debugger."
        }
    ],
    "version": "2.0.0"
}

Any idea on what may be causing this issue and how to solve it?

Thank you in advance

1 Answers1

0

Adding "-std=c++20" to "args" in tasks.json solved the issue.