Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this example of the use of the C++ 'explicit' keyword correct?

In a GoogleTechTalks video on Youtube, Bjarne Stroustrup talks about the upcoming C++0x standard. In the video he mentions the following example:

#include <iostream>

struct Sick
{
    Sick(double d)       { std::cout << d << "\n"; }
    explicit Sick(int i) { std::cout << i << "\n"; }
};


int main()
{
    Sick s1 = 2.1;
    Sick s2(2.1);
}

Did he mean to place the explicit keyword before Sick(double) rather than Sick(int), in order to highlight problems associated with implicit conversions in certain contexts?

like image 662
Truncheon Avatar asked Sep 19 '11 18:09

Truncheon


People also ask

What is explicit C?

The explicit function specifier controls unwanted implicit type conversions. It can only be used in declarations of constructors within a class declaration. For example, except for the default constructor, the constructors in the following class are conversion constructors.

What does explicit keyword 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 explicit keyword in Java?

The explicit -keyword can be used to enforce a constructor to be called explicitly. class C { public: explicit C() =default; }; int main() { C c; return 0; } the explicit -keyword in front of the constructor C() tells the compiler that only explicit call to this constructor is allowed.

What is implicit constructor in C++?

implicit constructor is a term commonly used to talk about two different concepts in the language, the. implicitly declared constructor which is a default or copy constructor that will be declared for all user classes if no user defined constructor is provided (default) or no copy constructor is provided (copy).


1 Answers

In his discussion, Stroustrup mentions that a direct initialization, such as

Sick s2(2.1);

will consider only constructors marked explicit if there are any explicit constructors. That's not my experience with several compilers (including GCC 4.6.1 and MSVC 16/VS 2010), and I can't find such a requirement in the standard (though I'd be interested if someone can point it out).

However, if ints are used in the initializers, I think the example would show what Stroustrup meant to demonstrate:

#include <iostream>

struct Sick
{
    Sick(double d)       { std::cout << "double " << d << "\n"; }
    explicit Sick(int i) { std::cout << "int " << i << "\n"; }
};


int main()
{
    Sick s1 = 2;
    Sick s2(2);
}

Running the above will display:

double 2
int 2

Shows that the two apparently equivalent initializations actually select different constructors.

(Or as Truncheon mentioned in the question - and I missed - that the explicit keyword should be on the Sick(double d) constructor).

like image 78
Michael Burr Avatar answered Oct 11 '22 08:10

Michael Burr