Kon*_*lph 6 c++ clang++ libc++
以下代码在g ++(各种版本)上编译良好,但在我的系统上使用libc ++的clang ++ - 3.4失败:
#include <map>
#include <string>
std::map<std::string, std::string> f() {
return {};
}
int main() {
auto m = f();
}
Run Code Online (Sandbox Code Playgroud)
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())
^
Run Code Online (Sandbox Code Playgroud)
实际上,include文件将构造函数声明为explicit.但它在我的C++ 11草案标准中没有标记.这是clang ++/libc ++中的错误吗?我无法找到相关的错误报告.
在C++ 14之前没有空构造函数.在C++ 14之前,默认构造std::map<Key, Value, Compare, Allocator>标explicit有2个默认参数:
explicit map( const Compare& comp = Compare(),
const Allocator& alloc = Allocator() );
Run Code Online (Sandbox Code Playgroud)
在C++ 14之后,我们有一个非explicit空的默认构造函数,它explicit从之前调用构造函数(现在没有默认Compare参数):
map() : map( Compare() ) {}
explicit map( const Compare& comp,
const Allocator& alloc = Allocator() );
Run Code Online (Sandbox Code Playgroud)
所以你的例子只有在C++ 14之后才有效.
资料来源:http://en.cppreference.com/w/cpp/container/map/map
| 归档时间: |
|
| 查看次数: |
412 次 |
| 最近记录: |