class Foo {
public:
Foo(explicit int);
Foo& operator=(int);
};
That's invalid. The syntax is
class Foo {
public:
explicit Foo(int);
Foo& operator=(int);
};
The difference is that the conversion constructor cannot be used for implicit conversions when you put explicit before it:
Foo f(10); // works
Foo f = 10; // doesn't work
The above doesn't have anything to do with an assignment operator you declared there. It is not used since that is an initialization (only constructors are used). The following will use the assignment operator:
Foo f;
f = 10;
And will use the default constructor of Foo (the one taking no arguments).
Edit: The questioner changed his question to the specific ways of whether
Foo f = 1; // called "copy initialization"
Foo f(1); // called "direct initialization"
Are the same. The answer is that they are equivalent to the following:
Foo f(Foo(1));
Foo f(1);
If and only if the conversion constructor taking the int is not declared with keyword explicit
, otherwise the first is a compiler error (see above). The compiler is allowed to elide (optimize out) the temporary passed to the copy constructor of Foo in the first case if all semantic restrictions are still safisfied, and even if the copy constructor has side effects. That especially includes a visible copy constructor.