【发布时间】:2020-06-28 07:26:57
【问题描述】:
我对 std::size 感到困惑。我做了人们所说的事情,确保 #include <iterator> 并使用 C++17 作为 cpp 标准。但是编译器仍然说“大小不是标准的成员”。
这是一个例子:
int array[] = {1, 2, 3, 4, 5};
std::cout<<std::size(array);
我正在使用 VS 代码,其中我将 GCC 作为我的编译器。以下是我的c_cpp_properties。也许我的配置有问题?
{
"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
}
我的g++ --version:“g++.exe (i686-posix-dwarf-rev0, 由 MinGW-W64 项目构建) 8.1.0”。
当我在终端中使用g++ -std=c++17 进行编译时,它可以工作。但是直接使用VS代码“运行构建任务”是行不通的。
我的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
}
}
]
}
【问题讨论】:
-
你的编译器不支持 C++17。
-
或者您没有使用启用 C++17 支持的选项。
-
如何编译?您是否设置了编译器标志?
-std=c++17 -
std::size() 肯定是 C++ 17 的一部分。我在自己的代码中使用它,包括 clang 7、GCC 9 和 MSVC 2019。
-
g++ --version为您输出了什么?