我有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++错误吗?如何绕过此错误?
发布于 2013-06-24 03:32:49
复制我在评论中说的话
,这是http://cplusplus.github.io/LWG/lwg-active.html#2193。我不确定“提议的决议”或类似的东西是否会出现在C++14中。在复制初始化上下文中使用显式默认构造函数的值初始化是否格式良好本身也是一个核心语言DR http://open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1518,它解释了可能的跨编译器差异。
如果您的实现实现了http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1494,并且以下是默认参数的有效语法,那么您可以这样做
const std::map<std::string, int>& db{}不幸的是,这是不允许的(我认为原因可能是显式地传递参数,你也不能直接初始化参数,所以为什么允许它作为默认参数呢?)所以在我看来,唯一的方法就是显式地创建它
const std::map<std::string, int>& db = std::map<std::string, int>{}决定你是否想要以可能的更多代码为代价摆脱冗余。一些替代方案
const std::map<std::string, int>& db = decltype(ContextSummary::db){}
const std::map<std::string, int>& db_ = decltype(db){}
const std::map<std::string, int>& db = std::decay<decltype(db)>::type{}https://stackoverflow.com/questions/17264067
复制相似问题