keywords:explicit

C++ Reference

explicit

When a constructor is specified as explicit, no automatic conversion will be used with that constructor – but parameters passed to the constructor may still be converted. For example:

    struct foo {
      explicit foo( int a )
        : a_( a )
      { }
 
      int a_;
    };
 
    int bar( const foo & f ) {
      return f.a_;
    }
 
    bar( 1 );  // fails because an implicit conversion from int to foo
               // is forbidden by explicit.
 
    bar( foo( 1 ) );  // works -- explicit call to explicit constructor.
 
    bar( static_cast<foo>( 1 ) );  // works -- call to explicit constructor via explicit cast.
 
    bar( foo( 1.0 ) );  // works -- explicit call to explicit constructor
                        // with automatic conversion from float to int.