Why do we need to make a constructor as explicit if it doesn't take any input?
That seems incorrect to me. A constructor needs to be explicit if you don't want the constructor to be called automatically.
Say you have:
struct Foo
{
Foo() {}
Foo(int) {}
};
void bar(Foo)
{
}
Then,
bar(1);
will work. It is translated as:
bar(Foo(1));
If there is a default constructor, i.e. one that does not take any arguments, there is nothing to convert from. You may not use:
bar();
and hope to get it translated as:
bar(Foo{});
What is the use of making a constructor as explicit if it takes more than one parameter and all the parameters are not default?
This one has some validity.
If you had:
struct Foo
{
Foo() {}
Foo(int, int) {}
};
void bar(Foo)
{
}
You may not use
bar(10, 20);
or
bar((10, 20));
and hope to have it translated as:
bar(Foo(10, 20));
However, you may use:
bar({10, 20});
and it will be translated as:
bar(Foo{10, 20});
If you want to prevent use of this syntax, bar({10, 20})
, you may make the constructor explicit
. IMO, this has less utility. The potential for inadvertently misusing a constructor with one argument is real. The potential for inadvertently misusing a constructor with more than one argument is very small.