Cited
:http://stackoverflow.com/
:http://en.cppreference.com/
What does the explicit keyword in C++ mean?
[00]
The explicit specifier specifies that a constructor or conversion function (since C++11) doesn't allow implicit conversions or copy-initialization. It may only appear within the decl-specifier-seq of the declaration of such a function within its class definition.
Notes
A constructor with a single non-default parameter (until C++11) that is declared without the function specifier explicit is called a converting constructor.
Both constructors (other than copy/move) and user-defined conversion functions may be function templates; the meaning of
explicit
doesn't change.struct A { A(int) { } // converting constructor A(int, int) { } // converting constructor (C++11) operator bool() const { return true; } }; struct B { explicit B(int) { } explicit B(int, int) { } explicit operator bool() const { return true; } }; int main() { A a1 = 1; // OK: copy-initialization selects A::A(int) A a2(2); // OK: direct-initialization selects A::A(int) A a3 {4, 5}; // OK: direct-list-initialization selects A::A(int, int) A a4 = {4, 5}; // OK: copy-list-initialization selects A::A(int, int) A a5 = (A)1; // OK: explicit cast performs static_cast if (a1) break; // OK: A::operator bool() bool na1 = a1; // OK: copy-initialization selects A::operator bool() bool na2 = static_cast<bool>(a1); // OK: static_cast performs direct-initialization // B b1 = 1; // error: copy-initialization does not consider B::B(int) B b2(2); // OK: direct-initialization selects B::B(int) B b3 {4, 5}; // OK: direct-list-initialization selects B::B(int, int) // B b4 = {4, 5}; // error: copy-list-initialization does not consider B::B(int,int) B b5 = (B)1; // OK: explicit cast performs static_cast if (b1) break; // OK: B::operator bool() // bool nb1 = b2; // error: copy-initialization does not consider B::operator bool() bool nb2 = static_cast<bool>(b2); // OK: static_cast performs direct-initialization }
[00]
In C++, the compiler is allowed to make one implicit conversion to resolve the parameters to a function. What this means is that the compiler can use constructors callable with a single parameterto convert from one type to another in order to get the right type for a parameter. Here's an example class with a constructor that can be used for implicit conversions:
class Foo
{
public:
// single parameter constructor, can be used as an implicit conversion
Foo (int foo) : m_foo (foo)
{
}
int GetFoo () { return m_foo; }
private:
int m_foo;
};
Here's a simple function that takes a
Foo
object:void DoBar (Foo foo)
{
int i = foo.GetFoo ();
}
and here's where the
DoBar
function is called.int main ()
{
DoBar (42);
}
The parameter is not a
Foo
object, but an int
. However, there exists a constructor for Foo
that takes an int
so this constructor can be used to convert the parameter to the correct type.
The compiler is allowed to do this once for each parameter.
Prefixing the
explicit
keyword to the constructor prevents the compiler from using that constructor for implicit conversions. Adding it to the above class will create a compiler error at the function call DoBar (42)
. It is now necessary to call for conversion explicitly with DoBar (Foo (42))
The reason you might want to do this is to avoid accidental construction that can hide bugs. Contrived example:
- You have a
MyString(int size)
class with a constructor that constructs a string of the given size. You have a functionprint(const MyString&)
, and you call it withprint(3)
. You expect it to print "3", but it prints an empty string of length 3 instead.
[01]
Suppose you have a class String:
class String {
public:
String(int n); // allocate n bytes to the String object
String(const char *p); // initializes object with char *p
};
Now if you try
String mystring = 'x';
the char 'x' will be implicitly converted to int and then will call the String(int) constructor. But this is not what the user might have intended. So to prevent such conditions, we shall define the constructor as
explicit
:class String {
public:
explicit String (int n); //allocate n bytes
String(const char *p); // initialize sobject with string p
};
[02]
In C++, a constructor with only one required parameter is considered an implicit conversion function. It converts the parameter type to the class type. Whether this is a good thing or not depends on the semantics of the constructor.For example, if you have a string class with constructorString(const char* s)
, that's probably exactly what you want. You can pass aconst char*
to a function expecting aString
, and the compiler will automatically construct a temporaryString
object for you.On the other hand, if you have a buffer class whose constructorBuffer(int size)
takes the size of the buffer in bytes, you probably don't want the compiler to quietly turnint
s intoBuffer
s. To prevent that, you declare the constructor with theexplicit
keyword:class Buffer { explicit Buffer(int size); ... }
That way,void useBuffer(Buffer& buf); useBuffer(4);
becomes a compile-time error. If you want to pass a temporaryBuffer
object, you have to do so explicitly:useBuffer(Buffer(4));
In summary, if your single-parameter constructor converts the parameter into an object of your class, you probably don't want to use theexplicit
keyword. But if you have a constructor that simply happens to take a single parameter, you should declare it asexplicit
to prevent the compiler from surprising you with unexpected conversions.
....
No comments:
Post a Comment