Yes, that is part of how it works, but there is more to it. It isn't only one-parameter constructors that can be explicit. You can do it for any constructor regardless of number of parameters, better explained by code:
#include <memory>
struct MyStruct1 {
bool myBool1 = false;
bool myBool2 = false;
explicit MyStruct1(bool val1 = false, bool val2 = false)
: myBool1(val1)
, myBool2(val2)
{}
};
void func (const MyStruct1& myStruct = {}) // This fails if constructor is explicit
{
// do something with struct
}
MyStruct1 func2 (bool a)
{
if (!a) {
return {}; // Returning default like this fails if constructor is explicit
}
return {true, false}; // Fails if constructor is explicit
}
int main()
{
auto msp = std::make_unique<MyStruct1>(true, false); // Perfect forwarding is always OK
func({true, false}); // Fails to compile if constructor is explicit
func(MyStruct1{true, false}); // Always OK
MyStruct1 ms1 = {true, false}; // Fails to compile if constructor is explicit
MyStruct1 ms2{true, false}; // Always OK
MyStruct1 ms3 = {}; // Fails if constructor is explicit
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…