Home / C/C++ / When to use “explicit” keyword

When to use “explicit” keyword

  • Use it when the constructor has one and only one argument.

» It has meaning only when applied to the constructor which can be called with exactly 1 argument provided. (Either it has 1 argument or has default values for all other)

  • Use it only on the default, copy, and move constructor.

» It is actually useless for them: default constructor does not have arguments, and move and copy constructors parameters already have the type of that class, so no implicit conversion could happen even in theory.

The gold rule is to use explicit keyword for all constructors which can be called with one argument provided unless you want to enable implicit conversion from the argument type


Download Project (VS2015):

Google Drive 1 Google Drive 2


About admin

Check Also

[ SC Tutorial ] 3. SystemC Data types

SystemC has a number of predefined data types to support hardware designs spanning from the …

Leave a Reply

Your email address will not be published. Required fields are marked *