Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我最近偶然发现了一个接收单个指针参数的显式构造函数。我想知道在这种情况下是否需要显式关键字?因为没有指针的构造函数,所以不能有任何隐式转换。
class Foo { public: explicit Foo(int* int_ptr); }
以下代码:
void f(Foo) {} int main() { int* p; f(p); }
编译失败explicit。
explicit
没有它就可以愉快地编译。
godbolt.org 上的实时示例