What is the 'explicit' keyword and why is it used for constructors?
Last updated
explicitstops a constructor (or conversion operator) from being used for implicit conversions, soWidget b = 5;andf(5)fail whileWidget a(5);works.It prevents surprising silent conversions, since any single-argument constructor otherwise doubles as an implicit conversion.
Default to marking single-argument constructors
explicit. Since C++11 it also applies to multi-arg (brace-list) constructors and conversion operators.