【问题标题】:std::size() not available in C++ 17std::size() 在 C++ 17 中不可用
【发布时间】: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 为您输出了什么?

标签: c++ c++17


【解决方案1】:

std::size() 可用starting from C++17

尝试为您的编译器启用 -std=c++17(您的 GCC 版本可能默认不支持 C++17。要启用 C++17 支持,请将命令行参数 -std=c++17 添加到您的 g++ 命令行)。

另外,对于 GCC 中的 C++17 支持,您可以参考C++17 Support in GCC

另外,请仔细检查源文件是否包含#include &lt;iterator&gt;(我知道你说过你已经检查过了,但仔细检查总是好的),直接或间接通过#include'ing以下任何标题:

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

【讨论】:

  • 谢谢,但我确定不包括任何其他头文件,因为这只是一个文件程序。
猜你喜欢
  • 2021-10-26
  • 2018-10-29
  • 2019-06-06
  • 1970-01-01
  • 2017-05-08
  • 2018-03-22
  • 1970-01-01
  • 2023-03-23
相关资源
最近更新 更多