【问题标题】:How to determine whether a class has a particular templated member function?如何确定一个类是否具有特定的模板化成员函数?
【发布时间】:2011-01-08 20:38:40
【问题描述】:

我想知道是否可以扩展 SFINAE 方法来检测一个类是否具有某个成员函数(如此处所述:

“C++ 中是否有一种技术可以知道一个类是否具有给定签名的成员函数?” Check if a class has a member function of a given signature

) 支持模板化成员函数?例如。能够检测到以下类中的函数 foo:

struct some_class {
   template < int _n > void foo() { }
};

我认为可以对 foo 的特定实例执行此操作,(例如,检查 void foo&lt; 5 &gt;() 是否为成员)如下:

template < typename _class, int _n >
class foo_int_checker {

  template < typename _t, void (_t::*)() >
  struct sfinae { };

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

  template < typename _t >
  static small
  test( ... );

public:

  enum { value = sizeof( test< _class >( 0 ) ) == sizeof( big ) };

};

然后执行foo_int_checker&lt; some_class, 5 &gt;::value 来检查some_class 是否有成员void foo&lt; 5 &gt;()。但是在 MSVC++ 2008 上,这总是返回 false 而 g++ 在 test( sfinae&lt; _t, &amp;_t::foo&lt; _n &gt; &gt; ); 行给出以下语法错误

test.cpp:24: error: missing `>' to terminate the template argument list
test.cpp:24: error: template argument 2 is invalid
test.cpp:24: error: expected unqualified-id before '<' token
test.cpp:24: error: expected `,' or `...' before '<' token
test.cpp:24: error: ISO C++ forbids declaration of `parameter' with no type

两者似乎都失败了,因为我试图从本身就是模板参数的类型中获取模板函数实例化的地址。有谁知道这是可能的还是由于某种原因被标准禁止?

编辑:似乎我错过了 ::template 语法来让 g++ 正确编译上述代码。如果我将获得函数地址的位更改为&amp;_t::template foo&lt; _n &gt;,则程序会编译,但我会得到与 MSVC++ 相同的行为(value 始终设置为false)。

如果我注释掉 test 的 ... 重载以强制编译器选择另一个,我会在 g++ 中收到以下编译器错误:

test.cpp: In instantiation of `foo_int_checker<A, 5>':
test.cpp:40:   instantiated from here
test.cpp:32: error: invalid use of undefined type `class foo_int_checker<A, 5>'
test.cpp:17: error: declaration of `class foo_int_checker<A, 5>'
test.cpp:32: error: enumerator value for `value' not integer constant

其中第 32 行是enum { value = sizeof( test&lt; _class &gt;( 0 ) ) == sizeof( big ) }; 行。不幸的是,这似乎并不能帮助我诊断问题:(。MSVC++ 给出了一个类似的无法描述的错误:

error C2770: invalid explicit template argument(s) for 'clarity::meta::big checker<_checked_type>::test(checker<_checked_type>::sfinae<_t,&_t::template foo<5>> *)'

在同一行。

奇怪的是,如果我从特定类而不是模板参数获取地址(即而不是 &amp;_t::template foo&lt; _n &gt; 我做 &amp;some_class::template foo&lt; _n &gt;)然后我得到正确的结果,但是我的检查器类仅限于检查该函数的单个类 (some_class)。另外,如果我执行以下操作:

template < typename _t, void (_t::*_f)() >
void
f0() { }

template < typename _t >
void
f1() {
  f0< _t, &_t::template foo< 5 > >();
}

并调用f1&lt; some_class &gt;(),然后我不会在&amp;_t::template foo&lt; 5 &gt; 上收到编译错误。这表明只有在 SFINAE 上下文中从本身是模板参数的类型中获取模板化成员函数的地址时才会出现问题。啊!

【问题讨论】:

标签: c++ sfinae


【解决方案1】:

在 Boost.MPL 中已经实现了类似的东西,它被称为“BOOST_MPL_HAS_XXX_TRAIT_DEF”。见:

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

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

另外,对于您的具体情况,不要将函数指针作为参数传递 (void (_t::*)()),而是尝试在方法体中使用它,例如:

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

【讨论】:

  • 感谢 Boost MPL 链接。虽然他们似乎没有检测到功能,但他们的方法是相似的,并且可以解决我遇到的问题。我现在只需要拆开它们的宏:) 我认为必须将函数指针作为方法签名的一部分获取,以便编译器将其用于重载解析。当我将它移到方法中时,编译器总是选择测试的“大”重载,因此即使函数不存在,“值”也为真。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-04
  • 1970-01-01
  • 2022-01-10
  • 1970-01-01
  • 2013-12-20
  • 1970-01-01
  • 2012-04-03
相关资源
最近更新 更多