使用clang ++ 4.2,"选择的构造函数在复制初始化错误中是显式的"

pro*_*eek 6 xcode clang c++11 clang++

我有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
Run Code Online (Sandbox Code Playgroud)

当我尝试编译这个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;
}
Run Code Online (Sandbox Code Playgroud)

我收到此错误消息.该代码适用于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...

                                           ^    ~~
Run Code Online (Sandbox Code Playgroud)

这是clang ++ bug吗?我该如何绕过这个错误?

Joh*_*itb 8

复制我在评论中所说的内容

这是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{}
Run Code Online (Sandbox Code Playgroud)

不幸的是,这是不允许的(我认为我明确地传递参数的原因是,你也不能直接初始化一个参数,所以为什么要允许它用于默认参数?).因此,在我看来,唯一的方法是明确创建它

const std::map<std::string, int>& db = std::map<std::string, int>{}
Run Code Online (Sandbox Code Playgroud)

决定是否要以可能更多的代码为代价来逃避冗余.一些替代品

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{}
Run Code Online (Sandbox Code Playgroud)