Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does "explicit" keyword have any effect on a default constructor? [duplicate]

Is there a reason to use the explicit keyword for a constructor that doesn't take any arguments? Does it have any effect? I'm wondering because I just came across the line

explicit char_separator() 

near the end of the page documenting boost::char_separator, but it's not explained any further there.

like image 457
Kilian Brendel Avatar asked Jul 21 '11 11:07

Kilian Brendel


People also ask

What does explicit constructor mean in C++?

The explicit keyword in C++ is used to mark constructors to not implicitly convert types. For example, if you have a class Foo − class Foo { public: Foo(int n); // allocates n bytes to the Foo object Foo(const char *p); // initialize object with char *p };

What is implicit default constructor?

A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values. If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameterless constructor A::A() .

How do you call a copy constructor in C++?

Copy Constructor in C++ClassName (const ClassName &old_obj); Copy constructor is used to initialize the members of a newly created object by copying the members of an already existing object. Copy constructor takes a reference to an object of the same class as an argument.

What is copy constructor explain with examples?

When Copy Constructor is called. Copy Constructor is called in the following scenarios: When we initialize the object with another existing object of the same class type. For example, Student s1 = s2, where Student is the class. When the object of the same class type is passed by value as an argument.


1 Answers

Reading explanation of members :

explicit char_separator(const Char* dropped_delims,                         const Char* kept_delims = "",                         empty_token_policy empty_tokens = drop_empty_tokens) explicit char_separator() 

The explicit keyword for the 1st constructor requires explicit creation of objects of char_separator type. What does the explicit keyword mean in C++? covers the explicit keyword very well.

The explicit keyword for the 2nd constructor is a noise and is ignored.

EDIT

From the c++ standard :

7.1.2 p6 tells :

The explicit specifier shall be used only in declarations of constructors within a class declaration; see 12.3.1.

12.3.1 p2 tells :

An explicit constructor constructs objects just like non-explicit constructors, but does so only where direct-initialization syntax (8.5) or where casts (5.2.9, 5.4) are explicitly used. A default constructor may be an explicit constructor; such a constructor will be used to perform default-initialization or value-initialization (8.5). [Example:

class Z { public: explicit Z(); explicit Z(int); // ... }; Z a;               // OK: default-initialization performed Z a1 = 1;          // error: no implicit conversion Z a3 = Z(1);       // OK: direct initialization syntax used Z a2(1);           // OK: direct initialization syntax used Z* p = new Z(1);   // OK: direct initialization syntax used Z a4 = (Z)1;       // OK: explicit cast used Z a5 = static_cast<Z>(1); // OK: explicit cast used 

—end example]

So, the default constructor with the explicit keyword is the same as without this keyword.

like image 152
BЈовић Avatar answered Sep 27 '22 19:09

BЈовић