"selected constructor is explicit in copy-initialization error" with clang ++ 4.2

I have 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 

When I tried to compile this C ++ 11 code:

 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 received this error message. Code works fine with 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 a clang ++ error? How can I get around this error?

+6
source share
1 answer

Copying what I said in my comment

This is http://cplusplus.imtqy.com/LWG/lwg-active.html#2193 . I am not sure if the "proposed solution" or something like that in C ++ 14 or not. The fact that value initialization using explicit default constructors in the context of copy initialization is well-formed or is also not the main language of DR http://open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1518 , which may explain the differences between compilers.

If your implementation implements http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1494 , and the following syntax is for the default argument, which you could do

 const std::map<std::string, int>& db{} 

Unfortunately, this is unacceptable (I think the reason is that I pass the argument explicitly, you cannot directly initialize the parameter, so why allow it for the default arguments?). Thus, it seems to me that the only way is to explicitly create it.

 const std::map<std::string, int>& db = std::map<std::string, int>{} 

Decide if you want to avoid redundancy with perhaps 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{} 
+8
source

Source: https://habr.com/ru/post/947951/


All Articles