【问题标题】:Can someone explain this template function declaration syntax有人可以解释这个模板函数声明语法吗
【发布时间】:2013-09-04 14:39:15
【问题描述】:

我不理解 boost::python 库中的以下模板声明(准确地说是 .../boost_1_51/boost/python/detail/msvc_typeinfo.hpp 的第 47 行):

template< typename T > T&(* is_ref_tester1(type<T>) )(type<T>) { return 0; }

其中typetemplate &lt;class T&gt; struct type {};

它在功能上似乎等同于:

template<typename T>
struct func_type_getter {
    typedef T&(*func_type)(type<T>);
};


template< typename T >
typename func_type_getter<T>::func_type is_ref_tester1(type<T>) { return 0; }

这些是等价的,只是简写,还是有人可以解释其中的差异?

【问题讨论】:

  • 是的,对于返回函数指针的函数来说,这是一个尴尬的“速记”。
  • 有趣的是,cdecl.org 阻塞了它(一旦你删除了它不支持的模板)。将T 替换为int 后即可使用。

标签: c++ function templates pointers


【解决方案1】:

是的,两者是等价的。以下是单行字的阅读方式:

template< typename T > T&(* is_ref_tester1(type<T>) )(type<T>) { return 0; }
                       ^           ^        ^            ^
                       |           |        |            |
                       |           |        |     3. it's return type is a pointer to a function taking a type<T>
                       |           |        |
                       |           |    2. it's a function taking a type<T>
                       |           |
                       |   1. this is the declared identifier
                       |
         4. this is the return type of the function whose pointer is returned

【讨论】:

  • 只是为了兴趣,我为什么不能写(为了清楚起见,删除模板的东西)int&amp;(*)(type&lt;int&gt;) is_ref_tester1(type&lt;int&gt;){ return 0; }
猜你喜欢
  • 1970-01-01
  • 2011-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多