名前空間
変種
操作

「cpp/language/explicit」の版間の差分

提供: cppreference.com
< cpp‎ | language
(r2.7.3) (ロボットによる 追加: de, en, es, fr, it, pt, ru, zh)
(Use {{lc}}. Update links. Various fixes.)
6行: 6行:
  
 
===構文===
 
===構文===
{{sdcl list begin}}
+
{{begin}}
{{sdcl list item | {{c|explicit}} {{sparam|class_name}} ( {{sparam|params}} ) }}
+
{{ | {{c|explicit}} {{|class_name}} ( {{|params}} ) }}
{{sdcl list item | {{c|explicit operator}} {{sparam|type}} ( ) {{mark since c++11}} }}
+
{{ | {{c|explicit operator}} {{|type}} ( ) {{mark since c++11}} }}
{{sdcl list end}}
+
{{end}}
  
 
===例===
 
===例===

2013年7月2日 (火) 13:54時点における版

 
 
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
メモリ確保
クラス
クラス固有の関数特性
explicit (C++11)
static
特別なメンバ関数
テンプレート
その他
 
コンストラクタと暗黙の型変換を許可していない(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
}