【发布时间】:2013-06-20 06:52:30
【问题描述】:
我有 clang++ 4.2
Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin11.4.2
Thread model: posix
当我尝试编译这段 c++11 代码时:
class ContextSummary {
int id;
int hops;
std::map<std::string, int> db {};
std::time_t timestamp;
ContextSummary(int id, const std::map<std::string, int>& db = {}, int hops = 3, std::time_t timestamp = 0)
{
this->id = id;
this->db = db;
this->hops = hops;
this->timestamp = timestamp;
}
我收到此错误消息。该代码适用于 g++4.8
error:
chosen constructor is explicit in copy-initialization
...id, const std::map<std::string, int>& db = {}, int hops = 3, std::time_t...
^ ~~
这是 clang++ 错误吗?如何绕过此错误?
【问题讨论】:
-
您正在尝试获取对临时值的引用。您用作默认参数的空映射仅对构造函数的调用是临时的。
-
@JoachimPileborg 如果
this->db是地图的实例——而不是地图的引用,这应该不是问题. @prosseef BTW,您应该在构造函数中使用初始化列表。不是矫揉造作。