नीचे एक प्रकार का गुण निर्धारित करना है कि कुछ फ़ंक्शन पॉइंटर और कुछ परीक्षण मामले हैं या नहीं। नोट, यह जांचने के लिए कि क्या कोई फ़ंक्शन पॉइंटर है, आपको std::is_pointer<P>::value
true
और std::is_function<T>::value
true
है जहां T
P
पॉइंटर हटा दिए जाने पर परीक्षण करने की आवश्यकता है। नीचे दिया गया कोड केवल यह करता है:
#include <type_traits>
#include <iostream>
#include <utility>
template <typename Fun>
struct is_fun_ptr
: std::integral_constant<bool, std::is_pointer<Fun>::value
&& std::is_function<
typename std::remove_pointer<Fun>::type
>::value>
{
};
template <typename Fun>
typename std::enable_if<is_fun_ptr<Fun>::value>::type
test(Fun) {
std::cout << "is a function pointer\n";
}
template <typename Fun>
typename std::enable_if<!is_fun_ptr<Fun>::value>::type
test(Fun) {
std::cout << "is not a function pointer\n";
}
void f0() {}
void f1(int) {}
void f2(int, double) {}
struct s0 { void operator()() {} };
struct s1 { void operator()(int) {} };
struct s2 { void operator()(int, double) {} };
int main()
{
int v0(0);
int* p0(&v0);
void (*p1)() = &f0;
void (**p2)() = &p1;
std::cout << "v0="; test(v0);
std::cout << "p0="; test(p0);
std::cout << "p1="; test(p1);
std::cout << "p2="; test(p2);
std::cout << "f0="; test(&f0);
std::cout << "f1="; test(&f1);
std::cout << "f2="; test(&f2);
std::cout << "s0="; test(s0());
std::cout << "s1="; test(s1());
std::cout << "s2="; test(s2());
std::cout << "l0="; test([](){});
std::cout << "l1="; test([](int){});
std::cout << "l2="; test([](int, double){});
}
स्रोत
2013-09-06 22:04:25
क्या आप दोनों is_function और is_pointer देख सकते हैं? –
क्यों SFINAE? यह यहाँ एक overkill है। –
क्यों कोई साधारण एफ (std :: function)? –