http://www.cppreference.com/wiki/keywords/explicit
When a constructor is specified as explicit, no automatic conversion
will be used with that constructor – but parameters passed to the
constructor may still be converted. For example:
struct foo {
explicit foo( int a )
: a_( a )
{ }
int a_;
};
int bar( const foo & f ) {
return f.a_;
}
bar( 1 ); // fails because an implicit conversion from int to foo
// is forbidden by explicit.
bar( foo( 1 ) ); // works -- explicit call to explicit
constructor.
bar( static_cast<foo>( 1 ) ); // works -- call to explicit
constructor via explicit cast.
bar( foo( 1.0 ) ); // works -- explicit call to explicit
constructor
// with automatic conversion from float to
int.
-----------------------------------------------------------------
class A {
public:
A(int a) : data(a){}
int data;
};
class B {
public:
//explicit B(int b) : data(b){}
B(int b) : data(b){}
int data;
};
void func_a() {
A a = 0;
a = 1;
}
void func_b() {
B b = 0;
b = 1;
}
int main() {
func_a();
func_b();
return 0;
}
-----------------------------------------------------------------
explicit.cpp: In function `void func_b()':
explicit.cpp:16: error: conversion from `int' to non-scalar type `B'
requested
explicit.cpp:17: error: no match for 'operator=' in 'b = 1'
explicit.cpp:6: note: candidates are: B& B::operator=(const B&)