std::enable_if – cppreference.com
Aus cppreference.com
<metanoindex/>
<tbody> </tbody>
| definiert in Header <type_traits> |
||
|
|
(seit C++11) | |
Wenn B ist true, std::enable_if hat einen öffentlichen Member typedef type gleich T, andernfalls gibt es kein Mitglied 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.
Diese metafunction verwendet wird, bedingt entfernen Funktionen und Klassen vor Überlastung Auflösung basierend auf Exterieur und separate Funktion Überlastungen und Spezialisierungen für verschiedene Exterieur bieten. std::enable_if kann als zusätzliche Funktion Argument (gilt nicht für überladene Operatoren) verwendet werden, als Rückgabetyp (gilt nicht für Konstruktoren und Destruktoren) oder als Klasse Vorlage oder Funktion Template-Parameter .
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.
Mitglied Typen
Type Original: Type The text has been machine-translated via Google Translate. |
Definition |
type
|
entweder keine oder solche Original: either The text has been machine-translated via Google Translate. |
Mögliche Implementierung
template<bool B, class T = void> struct enable_if {}; template<class T> struct enable_if<true, T> { typedef T type; };
Beispiel
#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: