whitel
whitel

Reputation: 13

std::size() not available in C++ 17

I am confused of std::size. I did something people said, making sure #include <iterator> and using C++17 as cpp standard. But the compiler still say "size is not a member of std". Here is an example:

int array[] = {1, 2, 3, 4, 5};
std::cout<<std::size(array);

I am using VS code, in which I take GCC as my compiler. Following is my c_cpp_properties. Perhaps something wrong in my configuration?

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "D:\\software\\mingw32\\bin\\gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x86"
        }
    ],
    "version": 4
}

My g++ --version : "g++.exe (i686-posix-dwarf-rev0, Built by MinGW-W64 project) 8.1.0".

When I use g++ -std=c++17 in the terminal to compile, it works. But directly using VS code "run build task" doesn't work.

My tasks.json:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "g++.exe build active file",
            "command": "D:\\software\\mingw32\\bin\\g++.exe",
            "args": [
                // "-std=c++17",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "D:\\software\\mingw32\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build"
        },  //why there are two blocks? I modified tasks.json it works well.
        {
            "type": "shell",
            "label": "g++.exe build active file",
            "command": "D:\\software\\mingw32\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "D:\\software\\mingw32\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

Upvotes: 0

Views: 1870

Answers (1)

user3980558
user3980558

Reputation:

std::size() is available starting from C++17.

Try enabling -std=c++17 for your compiler (your GCC version might not support C++17 by default. To enable C++17 support, add the command-line parameter -std=c++17 to your g++ command line).

Also, for C++17 support in GCC, you can refer to C++17 Support in GCC.

In addition, please double check that the source files contain #include <iterator> (I know you said you already checked this, but double checking is always good), either directly, or indirectly by #include'ing any of the following headers:

<array>
<deque>
<forward_list>
<list>
<map>
<regex>
<set>
<string>
<string_view>
<unordered_map>
<unordered_set>
<vector>

Upvotes: 2

Related Questions