带有多个参数的构造函数是否需要 explicit 关键字?

Is the explicit keyword needed with a constructor taking more than one parameter?

这个问题属于前面的C++11标准(C++03)。 explicit 防止从一种类型到另一种类型的隐式转换。例如:

struct Foo
{
    explicit Foo(int);
};

Foo f = 5; // will not compile
Foo b = Foo(5); // works

如果我们有一个带有两个或更多参数的构造函数,explicit 会阻止什么?我知道在 C++11 中你已经支持了初始化,所以它会阻止这样的构造:

struct Foo
{
    explicit Foo(int, int);
};

Foo f = {4, 2}; // error!

但是在 C++03 中我们没有大括号初始化,所以 explicit 关键字在这里阻止了什么样的构造?

如果有人使用默认参数更改您的方法的签名,这可能会很有趣:

struct Foo
{
    explicit Foo(int, int = 0);
};

使用 explicit 关键字,您惯用地说您永远不希望构造函数进行隐式转换。

If we have a constructor that takes two or more parameters, what will explicit prevent?

没有。