此模板中的类型是什么

What is the type in this template?

本文关键字:类型 是什么      更新时间:2023-10-16
std::tr1::_Bind<void (*()(std::tr1::reference_wrapper<int>))(int&)>

我理解std::tr1::reference_wrapper<int>,整个事情是某种函数指针,它返回void并将int&作为参数。但是我似乎一开始就无法遵循*()。代码是从我不久前经历的一些 gdb 会话中剪切粘贴的。

另外,tr1::函数的类型是什么?某个返回 void 并且不带参数的函数?

0x00000001000021a1 in std::tr1::function<void ()()>::operator() (this=0x7fff5fbffb98) at functional_iterate.h:865

但是,以下是一个错误:

template <typename T>
void f()
{ 
  cout << "generaln";
}
template<>
void f<void ()()> () // this is error
{
  cout << "specificn";
}
这是一个实例,std::tr1::_Bind实例化在一个函数的类型上,该

函数采用std::tr1::reference_wrapper<int>并返回指向一个函数,该函数引用了对int的引用并返回void。

阅读方法如下:

  • std::tr1::_Bind<类型>应该清楚。
  • type = void ( fn )(int&) 是一个接受int&并返回void的函数。
  • fn = * PTR,所以它实际上是指向函数的指针
  • PTR = ( fn2 )(std::tr1::reference_wrapper<int>) 是一个std::tr1::reference_wrapper<int>函数,到目前为止,我们拥有的是它的返回类型。
  • fn2 =(空),因为我们没有给该函数(类型)一个名称。

但是,正如我现在注意到的那样,当 fn2 为空时,它周围的括号可能也不应该存在(类似于您将函数类型"函数不带参数并返回 void"写为 void() ,而不是void()()

std::tr1::function的情况正是这样:一个不带参数并返回void的函数,空的"函数名称"周围有额外的括号。

好的,现在测试了它:gdb确实输出void()void()();这可能应该被认为是gdb错误。

因此,在C++中编写第一种类型的正确方法是:

std::tr1::_Bind<void (*(std::tr1::reference_wrapper<int>))(int&)>