Passing class to constructor, when no such constructor exists. Why does it work?

The compiler generates a copy constructor for you:

If no user-defined copy constructors are provided for a class type (struct, class, or union), the compiler will always declare a copy constructor as a non-explicit inline public member of its class.

You can make the copy constructor and assignment deleted and make the compiler not declare move assignment and constructor by declaring one of move constructor or assignment as deleted:

A(A&&) = delete; // Makes the class non-copyable and non-moveable.

It does have the copy constructor: the compiler has generated one for you.

If you want to disable that explicitly, then write

A(const A&) = delete;

in the class declaration; and using

A(A&&) = delete;

deletes all the rule of five functions, except the destructor.


An implicit copy constructor is generated by the Compiler, if you do not specify an own.

One further note:

Try

A a = 3.0f;

Conclusion: always mark constructors that take a single basic data type as explicit ... unless you like the implicit conversion