C++ Programming/Programming Languages/C++/Code/Keywords/explicit

The   keyword is used to avoid implicit type conversions.

Here's an example class with a constructor that can be used for implicit conversions:

Here's a simple function that takes a  object:

and here's where the  function is called.

The argument is not a  object, but an. However, there exists a constructor for  that takes an   so this constructor can be used to convert the parameter to the correct type.

The compiler is allowed to do this once for each parameter.

Prefixing the  keyword to the constructor prevents the compiler from using that constructor for implicit conversions. Adding it to the above class will create a compiler error at the function call. It is now necessary to call for conversion explicitly with