gpt4 book ai didi

c++ - std::map 默认构造函数是显式的吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:20:45 25 4
gpt4 key购买 nike

以下代码在 g++(各种版本)上编译良好,但在我的系统上使用 libc++ 的 clang++-3.4 上失败:

#include <map>
#include <string>

std::map<std::string, std::string> f() {
return {};
}

int main() {
auto m = f();
}

clang 标记了以下问题:

x.cpp:6:12: error: chosen constructor is explicit in copy-initialization
return {};
^~
/usr/local/Cellar/llvm34/3.4.2/lib/llvm-3.4/bin/../include/c++/v1/map:838:14: note: constructor declared here
explicit map(const key_compare& __comp = key_compare())
^

确实,包含文件将构造函数声明为显式但在我的 C++11 标准草案中并没有这样标记。 这是 clang++/libc++ 中的错误吗?我找不到相关的错误报告。

最佳答案

C++14之前没有空构造函数。 std::map<Key, Value, Compare, Allocator> 的默认构造标记为 explicit在 C++14 之前有 2 个默认参数:

explicit map( const Compare& comp = Compare(), 
const Allocator& alloc = Allocator() );

在 C++14 之后,我们有一个非 explicit调用 explicit 的空默认构造函数之前的构造函数(现在没有默认的 Compare 参数):

map() : map( Compare() ) {}
explicit map( const Compare& comp,
const Allocator& alloc = Allocator() );

因此您的示例仅在 C++14 之后有效。

来源:http://en.cppreference.com/w/cpp/container/map/map

关于c++ - std::map 默认构造函数是显式的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45029992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com