std::map 默认构造函数是显式的吗?
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())
^
确实,包含文件将构造函数声明为 explicit
。 但在我的 C++11 标准草案中并没有这样标记。 这是 clang++/libc++ 中的错误吗?我找不到相关的错误报告。
C++14之前没有空构造函数。 std::map<Key, Value, Compare, Allocator>
的默认构造被标记为 explicit
,带有 2 个默认参数,直到 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 之后有效。
以下代码在 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())
^
确实,包含文件将构造函数声明为 explicit
。 但在我的 C++11 标准草案中并没有这样标记。 这是 clang++/libc++ 中的错误吗?我找不到相关的错误报告。
C++14之前没有空构造函数。 std::map<Key, Value, Compare, Allocator>
的默认构造被标记为 explicit
,带有 2 个默认参数,直到 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 之后有效。