[英]Is the std::map default constructor explicit?
以下代码在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())
^
实际上,include文件将构造函数声明为explicit
。 但它在我的C ++ 11草案标准中没有标记。 这是clang ++ / libc ++中的错误吗? 我无法找到相关的错误报告。
在C ++ 14之前没有空构造函数。 std::map<Key, Value, Compare, Allocator>
默认构造用2个默认参数显explicit
标记,直到C ++ 14:
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之后才有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.