我正试图在算法中使用is_member_function_pointer,要么调用某个类型的特定成员函数(如果存在),要么什么都不做:
template< class T >
struct is_member_function_pointer;
检查T是否是非静态成员函数指针。如果T是非静态成员函数指针类型,则提供等于true的成员常数值。否则,值等于false。
例如:
#include <iostream>
#include <type_traits>
#include <vector>
struct A
{
void boo() const { std::cout << "boo." << std::endl; }
};
struct B {};
template<bool> struct booable;
template<>
struct booable<true> {
template<typename Type>
static void booIt(Type const & t)
{
t.boo();
}
};
template<>
struct booable<false> {
template<typename Type>
static void booIt(Type const & t)
{
std::cout << "booing a non-booable type" << std::endl;
}
};
template<typename Type>
void alg(Type const& t)
{
booable<std::is_member_function_pointer<decltype(&Type::boo)>::value>::booIt(t);
}
int main(int argc, const char *argv[])
{
A a;
B b;
alg(a);
alg(b);
return 0;
}
结构A
是布尔值,而B
不是。在算法alg
中,根据Type
是否实现成员函数boo
的事实,is_member_function_pointer
布尔值被设置为false
或true
。然后,该布尔值用于专门化booable
结构,该结构实现booIt
静态成员函数,其唯一目的是在布尔对象上调用boo
,或者不执行任何操作并通知用户。
但是,编译它(保存在main.cpp
中)会导致以下编译时错误:
main.cpp: In instantiation of ‘void alg(const Type&) [with Type = B]’:
main.cpp:46:10: required from here
main.cpp:37:54: error: ‘boo’ is not a member of ‘B’
booable<std::is_member_function_pointer<decltype(&Type::boo)>::value>::booIt(t);
这让我想知道:将这个报告为可用的布尔值而不是编译时错误难道不是这个特征结构的意义吗?这和我简单地做会得到的错误是一样的
B b;
b.boo();
我在这里做错了什么?
将其报告为可用的布尔值而不是编译时错误不是这个特征结构的重点吗?
是的,但为了测试类的某个成员,该成员需要存在。
您会得到错误,因为B
没有boo
,实例化alg
会导致decltype(&B::boo)
。你不会期望decltype(&int::foo)
编译,是吗?
你需要的是一个检查成员是否存在的特征。参见
- 是否可以编写一个C++模板来检查函数的存在
- 检查类是否具有给定签名的成员函数
例如。
您可以使用检查的结果来专门化进行进一步测试的模板。
编辑:
以下是使用表达式SFINAE:检查boo
的简单方法
template<typename T>
constexpr auto is_booable(int) -> decltype(std::declval<T>().boo(), bool())
{
return true;
}
template<typename T>
constexpr bool is_booable(...)
{
return false;
}
用法:
booable< is_booable<Type>(0) >::booIt(t);
以及现场示例。