Name
explicit specifier — Explicit constructor specifier
Synopsis
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
Get C++ In a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.