explicit specifier

explicit specifier Explicit constructor specifier

function-specifier := explicit

The explicit specifier can be used with a constructor to prevent implicit type conversions. It is permitted for any constructor but makes sense only for constructors that can be called with a single argument. An explicit constructor can be invoked from a declaration that uses function-like initialization or from an explicit type cast but not from a declaration that uses assignment-like initialization, nor from an implicit type cast.

Example

struct point {

  explicit point(int x, int y = 0);

};

point p1(10);  // OK

point p2 = 10; // Error: would be OK without explicit

point p3;

p3 = 20;       // Error: would be OK without explicit

p3 = static_cast<point>(40); // OK

See Also

class, declaration, function, static_cast, type, Chapter 2, Chapter 6