explicit [C++ Reference]
Keywords: keywords,explicit
Date: 2008-11-08T11:33:55-0800

[[explicit]]
C++ Reference
Translations of this page?:

- en
- br-pt
- cn
- cz
- de
- es
- fr
- it
- jp
- nl
- pl
- ro
- ru
- sk
- tr
- tw

## 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.

Edit this page • Old revisions
• • • Login • Index • Recent changes • RSS • cc •