为什么双参数构造函数以显式关键字开头?

lul*_*yon 16 c++ explicit

我的好友和我最近一直在阅读leveldb源代码.我们遇到了这个问题.在leveldb db/skiplist.h文件中,有一个构造函数声明:

explicit SkipList(Comparator cmp, Arena* arena);
Run Code Online (Sandbox Code Playgroud)

我知道带有单个参数的显式构造函数意味着没有构造函数参数的隐式类型转换.但是具有显式关键字的双参数构造函数意味着什么?这是C++ 11的新规则吗?

谢谢.

chr*_*ris 16

使用C++ 11,您可以使用braced-init-lists代替其他一些表达式,这会产生影响.例如,您可以在return语句中使用它们:

SkipList foo() {
    return {{}, nullptr}; //does not compile with explicit constructor
    return SkipList{{}, nullptr}; //compiles with or without explicit constructor
}
Run Code Online (Sandbox Code Playgroud)