q[explicit] 2 ways to call ctor

A conversion ctor can be invoked 2 ways
B b1 = 123;
B b2 (123);

Similarly, a copy ctor can be triggered 2 ways
B b1 = anotherB;
B b2 (anotherB);

The “explicit” keyword on a ctor disallows the “=” version….

Q(Obscure): why put “explicit” on the no-arg ctor?
A: I see no practical usage. https://stackoverflow.com/questions/48677746/explicit-default-constructor shows an example and some explanation.

struct C{
struct K{
    C c;
int main(){
    K myk{}; //fails with error: converting to 'C' from initializer list would use explicit constructor 'C::C()'

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s