xcode “选择的构造函数在复制初始化错误中是显式的”与 clang++ 4.2
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17264067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"chosen constructor is explicit in copy-initialization error" with clang++ 4.2
提问by prosseek
I have clang++ 4.2
我有铛++ 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
When I tried to compile this c++11 code:
当我尝试编译这个 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;
}
I got this error message. The code works fine with g++4.8
我收到此错误消息。该代码适用于 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...
^ ~~
Is this clang++ bug? How can I bypass this error?
这是clang ++错误吗?我怎样才能绕过这个错误?
回答by Johannes Schaub - litb
Copying what I said in my comment
复制我在评论中说的话
This is http://cplusplus.github.io/LWG/lwg-active.html#2193. I'm not sure whether the "proposed resolution" or something equivalent makes it into C++14 or not. The fact whether or not value initialization using explicit default constructors in a copy initialization context is wellformed or not itself is also a core-language DR http://open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1518, which explains possibly cross-compiler differences.
这是http://cplusplus.github.io/LWG/lwg-active.html#2193。我不确定“建议的解决方案”或等效的东西是否使它成为 C++14。在复制初始化上下文中使用显式默认构造函数的值初始化是否格式正确这一事实本身也是核心语言 DR http://open-std.org/jtc1/sc22/wg21/docs/cwg_active.html# 1518,这解释了可能的交叉编译器差异。
If your implementation implements http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1494and the following would be valid syntax for a default argument you could have done
如果您的实现实现了http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1494并且以下将是您可以完成的默认参数的有效语法
const std::map<std::string, int>& db{}
Unfortunately, this is not allowed (I think the reason my be that passing an argument explicitly, you can't direct-initialize a parameter either, so why allow it for default arguments?). So it appears to me the only way is to explicit create it
不幸的是,这是不允许的(我认为我的原因是显式传递参数,您也不能直接初始化参数,那么为什么要允许它作为默认参数?)。所以在我看来唯一的方法是显式创建它
const std::map<std::string, int>& db = std::map<std::string, int>{}
Decide whether you wanna get away with the redundancy at the cost of possibly more code. Some alternatives
决定是否要以可能会增加更多代码为代价来摆脱冗余。一些替代品
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{}