名前空間
変種

explicit specifier

提供: cppreference.com
2012年10月26日 (金) 14:00時点におけるTranslationBot (トーク | 投稿記録)による版 (Translated from the English version using Google Translate)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)

<metanoindex/>

 
 
C++言語
一般的なトピック
フロー制御
条件付き実行文
繰り返し文 (ループ)
ジャンプ文
関数
関数宣言
ラムダ関数宣言
inline 指定子
例外指定 (C++20未満)
noexcept 指定子 (C++11)
例外
名前空間
指定子
decltype (C++11)
auto (C++11)
alignas (C++11)
記憶域期間指定子
初期化
代替表現
リテラル
ブーリアン - 整数 - 浮動小数点
文字 - 文字列 - nullptr (C++11)
ユーザ定義 (C++11)
ユーティリティ
属性 (C++11)
typedef 宣言
型エイリアス宣言 (C++11)
キャスト
暗黙の変換 - 明示的な変換
static_cast - dynamic_cast
const_cast - reinterpret_cast
メモリ確保
クラス
クラス固有の関数特性
特別なメンバ関数
テンプレート
その他
 
コンストラクタと暗黙の型変換を許可していない(C++11以上)変換演算子を指定します
Original:
Specifies constructors and (C++11以上) conversion operators that don't allow implicit conversions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

構文

explicit class_name ( params )
explicit operator type ( ) (C++11以上)

struct A
{
    A ( int ) {}

    operator int() const { return 0; }
};

struct B
{
    explicit B(int) {}
    explicit operator int() const { return 0; }
};

int main()
{
    // A is has no explicit ctor / conversion, everything is fine
    A a1 = 1;
    A a2 ( 2 );
    A a3 { 3 };
    int na1 = a1;
    int na2 = static_cast<int>( a1 );
 
    B b1 = 1; // Error: implicit conversion from int to B
    B b2 ( 2 ); // OK: explicit constructor call
    B b3 { 3 }; // OK: explicit constructor call
    int nb1 = b2; // Error: implicit conversion from B to int
    int nb2 = static_cast<int>( b2 ); // OK: explicit cast
}