std::enable_if
提供:cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
| Defined in header <type_traits>
|
||
| template< bool B, class T = void > struct enable_if; |
(C + + 11以来) | |
もし
Bですtrue、std::enable_iftypeに等しいpublicメンバのtypedefTを持ち、そうでない場合には、typedefなメンバはありません. Original:
If
B is true, std::enable_if has a public member typedef type, equal to T; otherwise, there is no member typedef. The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
このメタ関数は、条件付きで型の特徴に基づいて、オーバーロードの解決から関数やクラスを削除するには、別の型の特徴ごとに別々の関数オーバーロードと特殊化を提供するために使用されます。 std::enable_ifは、戻り値の型(コンストラクタとデストラクタには適用されません)として、あるいはクラステンプレートまたは関数テンプレートパラメータとして、追加の関数の引数(演算子のオーバーロードには適用されません)として使用することができます.
Original:
This metafunction is used to conditionally remove functions and classes from overload resolution based on type traits and to provide separate function overloads and specializations for different type traits. std::enable_if can be used as an additional function argument (not applicable to operator overloads), as a return type (not applicable to constructors and destructors), or as a class template or function template parameter.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
目次 |
[編集] メンバータイプ
| タイプ
Original: Type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
Definition |
type
|
Tの値に応じてBまたはそのような会員はいません、どちらかOriginal: either T or no such member, depending on the value of BThe text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
[編集] 可能な実装
template<bool B, class T = void> struct enable_if {}; template<class T> struct enable_if<true, T> { typedef T type; }; |
[編集] 例
#include <type_traits> #include <iostream> // foo1 overloads are enabled via the return type template<class T> typename std::enable_if<std::is_floating_point<T>::value, T>::type foo1(T t) { std::cout << "foo1: float\n"; return t; } template<class T> typename std::enable_if<std::is_integral<T>::value, T>::type foo1(T t) { std::cout << "foo1: int\n"; return t; } // foo2 overload is enabled via a parameter template<class T> T foo2(T t, typename std::enable_if<std::is_integral<T>::value >::type* = 0) { return t; } // foo3 overload is enabled via a template parameter template<class T , class = typename std::enable_if<std::is_integral<T>::value>::type > T foo3(T t) // note, function signature is unmodified { return t; } // A is enabled via a template parameter template<class T, class Enable = void> class A; // undefined template<class T> class A<T, typename std::enable_if<std::is_floating_point<T>::value >::type> { }; int main() { foo1(1.2); // OK, calls the first version of foo1() foo1(10); // OK, calls the second version of foo1() // foo2(0.1); // compile-time error foo2(7); // OK // A<int> a1; // compile-time error A<double> a1; // OK }
Output:
foo1: float foo1: int