What is the type of this template?

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

I understand std::tr1::reference_wrapper<int> , and this whole thing is a kind of function pointer that returns void and takes int& as an argument. But I cannot follow * () at the beginning. Code cut from some gdb session went back a while.

Also, what is the type of function tr1 ::? Some function that returns void and takes no arguments?

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

But then an error occurs:

 template <typename T> void f() { cout << "general\n"; } template<> void f<void ()()> () // this is error { cout << "specific\n"; } 
+6
source share
1 answer

This is an instance of std::tr1::_Bind , created by the type of a function that takes std::tr1::reference_wrapper<int> and returns a pointer to a function that refers to int and returns void.

Here's how to read it:

  • std::tr1::_Bind< type > should be clear.
  • type = void ( fn )(int&) is a function that takes int& and returns void .
  • fn = * ptr, so it is actually a function pointer
  • ptr = ( fn2 )(std::tr1::reference_wrapper<int>) is a function that takes std::tr1::reference_wrapper<int> , and what we still have is a return type.
  • fn2 = (empty) because we do not give this function (type) a name.

However, as I have now noticed, when fn2 is empty, the parentheses around it probably shouldn't be there either (just like you write a function like โ€œfunction without parameters and returning voidโ€ as void() , not void()() >.

The case in std::tr1::function is exactly that: the function takes no parameters and returns void , with extra brackets around the empty "function name".

OK, now it is tested: gdb really outputs void() as void()() ; this should probably be seen as a gdb error.

So the correct way to write the first type in C ++ is:

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

Source: https://habr.com/ru/post/913264/


All Articles