std:: is_function
|
Definiert im Header
<type_traits>
|
||
|
template
<
class
T
>
struct is_function ; |
(seit C++11) | |
std::is_function
ist ein
UnaryTypeTrait
.
Prüft, ob
T
ein Funktionstyp ist. Typen wie
std::
function
, Lambdas, Klassen mit überladenem
operator()
und Zeiger auf Funktionen zählen nicht als Funktionstypen. Bietet die Member-Konstante
value
, die gleich
true
ist, wenn
T
ein Funktionstyp ist. Andernfalls ist
value
gleich
false
.
Wenn das Programm Spezialisierungen für
std::is_function
oder
std::is_function_v
hinzufügt, ist das Verhalten undefiniert.
Inhaltsverzeichnis |
Template-Parameter
| T | - | ein zu prüfender Typ |
Hilfsvariablen-Template
|
template
<
class
T
>
constexpr bool is_function_v = is_function < T > :: value ; |
(seit C++17) | |
Geerbt von std:: integral_constant
Member-Konstanten
|
value
[static]
|
true
wenn
T
ein Funktionstyp ist,
false
andernfalls
(öffentliche statische Member-Konstante) |
Member-Funktionen
|
operator bool
|
konvertiert das Objekt zu
bool
, gibt
value
zurück
(öffentliche Member-Funktion) |
|
operator()
(C++14)
|
gibt
value
zurück
(öffentliche Member-Funktion) |
Member-Typen
| Typ | Definition |
value_type
|
bool |
type
|
std:: integral_constant < bool , value > |
Hinweise
std::is_function
kann auf viel einfachere Weise implementiert werden. Implementierungen ähnlich der folgenden werden von neueren Versionen von
libc++
,
libstdc++
und
MS STL
verwendet:
template<class T> struct is_function : std::integral_constant< bool, !std::is_const<const T>::value && !std::is_reference<T>::value > {};
Die unten gezeigte Implementierung dient pädagogischen Zwecken, da sie die vielfältigen Arten von Funktionstypen veranschaulicht.
Mögliche Implementierung
// primary template template<class> struct is_function : std::false_type {}; // Spezialisierung für reguläre Funktionen template<class Ret, class... Args> struct is_function<Ret(Args...)> : std::true_type {}; // Spezialisierung für variadische Funktionen wie std::printf template<class Ret, class... Args> struct is_function<Ret(Args......)> : std::true_type {}; // Spezialisierung für Funktionstypen mit CV-Qualifizierern template<class Ret, class... Args> struct is_function<Ret(Args...) const> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) volatile> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) const volatile> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) const> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) volatile> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) const volatile> : std::true_type {}; // Spezialisierung für Funktionstypen mit Ref-Qualifiers template<class Ret, class... Args> struct is_function<Ret(Args...) &> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) const &> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) volatile &> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) const volatile &> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) &> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) const &> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) volatile &> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) const volatile &> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) &&> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) const &&> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) volatile &&> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) const volatile &&> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) &&> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) const &&> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) volatile &&> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) const volatile &&> : std::true_type {}; // Spezialisierungen für noexcept-Versionen aller oben genannten (C++17 und später) template<class Ret, class... Args> struct is_function<Ret(Args...) noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) const noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) volatile noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) const volatile noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) const noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) volatile noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) const volatile noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) & noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) const & noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) volatile & noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) const volatile & noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) & noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) const & noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) volatile & noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) const volatile & noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) && noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) const && noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) volatile && noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) const volatile && noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) && noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) const && noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) volatile && noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) const volatile && noexcept> : std::true_type {}; |
Beispiel
#include <functional> #include <type_traits> int f(); static_assert(std::is_function_v<decltype(f)>); static_assert(std::is_function_v<int(int)>); static_assert(!std::is_function_v<int>); static_assert(!std::is_function_v<decltype([]{})>); static_assert(!std::is_function_v<std::function<void()>>); struct O { void operator()() {} }; static_assert(std::is_function_v<O()>); struct A { static int foo(); int fun() const&; }; static_assert(!std::is_function_v<A>); static_assert(std::is_function_v<decltype(A::foo)>); static_assert(!std::is_function_v<decltype(&A::fun)>); template<typename> struct PM_traits {}; template<class T, class U> struct PM_traits<U T::*> { using member_type = U; }; int main() { using T = PM_traits<decltype(&A::fun)>::member_type; // T ist int() const& static_assert(std::is_function_v<T>); }
Siehe auch
|
prüft, ob ein Typ mit den gegebenen Argumenttypen aufgerufen werden kann (als ob durch
std::invoke
)
(Klassentemplate) |
|
|
(C++11)
|
prüft, ob ein Typ ein Objekttyp ist
(Klassentemplate) |
|
(C++11)
|
prüft, ob ein Typ ein Nicht-Union-Klassentyp ist
(Klassentemplate) |