explicit specifier
De cppreference.com
<metanoindex/>
Especifica construtores e operadores de conversão (desde C++11) que não permitem conversões implícitas
Original:
Specifies constructors and (desde C++11) conversion operators that don't allow implicit conversions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Sintaxe
Exemplo
struct A
{
A ( int ) {}
operator int() const { return 0; }
};
struct B
{
explicit B(int) {}
explicit operator int() const { return 0; }
};
int main()
{
// A is has no explicit ctor / conversion, everything is fine
A a1 = 1;
A a2 ( 2 );
A a3 { 3 };
int na1 = a1;
int na2 = static_cast<int>( a1 );
B b1 = 1; // Error: implicit conversion from int to B
B b2 ( 2 ); // OK: explicit constructor call
B b3 { 3 }; // OK: explicit constructor call
int nb1 = b2; // Error: implicit conversion from B to int
int nb2 = static_cast<int>( b2 ); // OK: explicit cast
}
