简体   繁体   English

std::size() 在 C++ 17 中不可用

[英]std::size() not available in C++ 17

I am confused of std::size.我对 std::size 感到困惑。 I did something people said, making sure #include <iterator> and using C++17 as cpp standard.我做了一些人们所说的,确保#include <iterator>并使用 C++17 作为 cpp 标准。 But the compiler still say "size is not a member of std".但是编译器仍然说“size 不是 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.我正在使用 VS 代码,其中我将 GCC 作为我的编译器。 Following is my c_cpp_properties .以下是我的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".我的g++ --version :“g++.exe(i686-posix-dwarf-rev0,由 MinGW-W64 项目构建)8.1.0”。

When I use g++ -std=c++17 in the terminal to compile, it works.当我在终端中使用g++ -std=c++17进行编译时,它可以工作。 But directly using VS code "run build task" doesn't work.但是直接使用VS代码“运行构建任务”是行不通的。

My tasks.json :我的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
            }
        }
    ]
}

std::size() is available starting from C++17 . std::size()从 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).尝试为您的编译器启用-std=c++17 (您的 GCC 版本可能默认不支持 C++17。要启用 C++17 支持,请将命令行参数-std=c++17到您的 g++命令行)。

Also, for C++17 support in GCC, you can refer to C++17 Support in GCC .此外,对于 GCC 中的 C++17 支持,您可以参考 GCC 中的C++17 支持

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:此外,请直接或间接通过 #include 包含以下任何标题来仔细检查源文件是否包含#include <iterator> (我知道您说过您已经检查过这一点,但仔细检查总是好的):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM