名前空間
変種
操作

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

提供: cppreference.com
< cpp‎ | language
(Use {{lc}}. Update links. Various fixes.)
1行: 1行:
{{tr_note}}
+
{{title|explicit }}
{{title|explicit specifier}}
+
{{cpp/language/navbar}}
{{cpp/language/navbar}}
+
 
+
{{tr|コンストラクタと暗黙の型変換を許可していない{{mark since c++11}}変換演算子を指定します|Specifies constructors and {{mark since c++11}} conversion operators that don't allow implicit conversions}}
+
 
+
===構文===
+
 
{{sdsc begin}}
 
{{sdsc begin}}
{{sdsc | {{c|explicit}} {{spar|class_name}} ( {{spar|params}} ) }}
+
{{sdsc|
{{sdsc  | {{c|explicit operator}} {{spar|type}} ( ) {{mark since c++11}} }}
+
{{|explicit}}
 +
 +
{{|{{}}
 +
{{|explicit }} {{spar|}} {{}}
 +
}}
 
{{sdsc end}}
 
{{sdsc end}}
 +
 +
 +
 +
  
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 
===例===
 
===例===
 
{{example|code=
 
{{example|code=
 
struct A
 
struct A
 
{
 
{
     A ( int ) {}
+
     A(int) { }
 
+
     operator int() const { return 0; }
+
     operator () const { return ; }
 
};
 
};
  
 
struct B
 
struct B
 
{
 
{
     explicit B(int) {}
+
     explicit B(int) { }
     explicit operator int() const { return 0; }
+
     explicit int
 +
() const { return ; }
 
};
 
};
  
 
int main()
 
int main()
 
{
 
{
    // A is has no explicit ctor / conversion, everything is fine
+
     A a1 = 1;
     A a1 = 1;
+
     A a2(2);
     A a2 ( 2 );
+
     A a3 {};
     A a3 { 3 };
+
     int
     int na1 = a1;
+
     int na2 = static_cast<int>( a1 );
+
+
na1 = a1;
    B b1 = 1; // Error: implicit conversion from int to B
+
     na2 = static_cast<>(a1);
     B b2 ( 2 ); // OK: explicit constructor call
+
 
     B b3 { 3 }; // OK: explicit constructor call
+
B b1 = 1; // :B
    int nb1 = b2; // Error: implicit conversion from B to int
+
     B b2(2); // :
     int nb2 = static_cast<int>( b2 ); // OK: explicit cast
+
     B b3 {}; // :
 +
int
 +
=
 +
b2; // :
 +
B
 +
     nb2 = static_cast<>(b2); //
 
}
 
}
 
}}
 
}}
  
[[de:cpp/language/explicit]]
+
deenesfritptruzh
[[en:cpp/language/explicit]]
+
[[es:cpp/language/explicit]]
+
[[fr:cpp/language/explicit]]
+
[[it:cpp/language/explicit]]
+
[[pt:cpp/language/explicit]]
+
[[ru:cpp/language/explicit]]
+
[[zh:cpp/language/explicit]]
+

2018年10月3日 (水) 04:28時点における版