且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何确定类是否有特定的模板成员函数?

更新时间:2023-11-27 18:40:52

在Boost.MPL中已经实现了类似的东西,它被称为BOOST_MPL_HAS_XXX_TRAIT_DEF。请参阅:

There is something similar already implemented in Boost.MPL, it is called "BOOST_MPL_HAS_XXX_TRAIT_DEF". See:

http://www.boost.org/doc/libs/1_41_0/libs/mpl/doc/refmanual/has-xxx-trait-def.html

它可以检测类是否具有给定的类型

It can detect if the class have a given named type.

对于你的具体情况,而不是传递函数指针作为参数(void(_t :: *)()),尝试使用它在你的方法的主体,即像:

Also, for your specific case, instead of passing the function pointer as a parameter (void (_t::*)()), try to use it in the body of your method, i.e., something like:

template < typename _t >
static big test( sfinae<_t> )
{
  &_t::foo<_n>;
}