c++ - std::size() 在 C++ 17 中不可用
问题描述
我对 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
}
}
]
}
解决方案
std::size()
从 C++17 开始可用。
尝试-std=c++17
为您的编译器启用(默认情况下,您的 GCC 版本可能不支持 C++17。要启用 C++17 支持,请将命令行参数添加-std=c++17
到您的 g++ 命令行)。
此外,对于 GCC 中的 C++17 支持,您可以参考 GCC 中的C++17 支持。
此外,请#include <iterator>
直接或间接地通过#include'ing 以下任何标头来仔细检查源文件是否包含(我知道您说过您已经检查过,但仔细检查总是好的):
<array>
<deque>
<forward_list>
<list>
<map>
<regex>
<set>
<string>
<string_view>
<unordered_map>
<unordered_set>
<vector>
推荐阅读
- excel - VBA - 将 DateVariable 传递给 Autofilter 失败
- javascript - 如果字符串仅包含空格,则匹配的正则表达式?
- python-3.x - Python中的季节性趋势分解?
- flask - jinja2.exceptions.UndefinedError: 'form' 未使用烧瓶和 WTF 定义
- json - 在 ReactJS 中设置多个搜索过滤器
- sql - 如何在没有 UNION 的情况下组合来自 2 个左连接的数据
- javascript - 将函数作为参数传递是如何在这里工作的?
- python - 在 keras 中绘制曲线给出 KeyError: 'accuracy'
- flutter - 如何使用 dart 包 pdf 从图像 dart 命令行创建 pdf
- postgresql - 通过匹配另一个表的所有列来更新一个表