How it works...

To understand why explicit constructors are necessary and how they work, we will first look at converting constructors. The following class has three constructors: a default constructor (without parameters), a constructor that takes an int, and a constructor that takes two parameters, an int and a double. They don't do anything, except printing a message. As of C++11, these are all considered converting constructors. The class also has a conversion operator that converts the type to a bool:

    struct foo     {       foo()      { std::cout << "foo" << std::endl; }      foo(int const a)      { std::cout << "foo(a)" << std::endl; }      foo(int const a, double const b)      { std::cout << "foo(a, b)" << std::endl; }       operator bool() const { return true; }  }; ...

Get Modern C++: Efficient and Scalable Application Development now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.