Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the type in this template?

Tags:

c++

templates

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

I understand std::tr1::reference_wrapper<int> and this whole thing is some sort of function pointer that returns void and takes int& as argument. But I can't seem to follow the *() in the beginning. The code's cut-pasted from some gdb session i was going through a while back.

Also, what is the type to tr1::function? Some function which returns void and takes no argument?

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

But then the following is an error:

template <typename T>
void f()
{ 
  cout << "general\n";
}

template<>
void f<void ()()> () // this is error
{
  cout << "specific\n";
}
like image 979
Fanatic23 Avatar asked Apr 15 '12 19:04

Fanatic23


People also ask

What is a template type?

There are three kinds of templates: function templates, class templates and, since C++14, variable templates. Since C++11, templates may be either variadic or non-variadic; in earlier versions of C++ they are always non-variadic.

What are the two types of templates?

There are two types of templates in C++, function templates and class templates.

What is a template example?

A template is a form, mold or pattern used as a guide to make something. Here are some examples of templates: Website design. Creating a document. Knitting a sweater.

What is the use of a template?

Templates are pre-formatted documents designed to create commonly used document types such as letters, fax forms, or envelopes.


1 Answers

This is an instance of std::tr1::_Bind instantiated on the type of a function taking a std::tr1::reference_wrapper<int> and returning a pointer to a function taking a reference to int and returning void.

Here's how to read it:

  • std::tr1::_Bind<type> should be clear.
  • type = void (fn)(int&) is a function taking int& and returning void.
  • fn = *ptr so it's actually a pointer to function
  • ptr = (fn2)(std::tr1::reference_wrapper<int>) is a function taking std::tr1::reference_wrapper<int> and what we had up to now is its return type.
  • fn2 = (empty) because we don't give that function (type) a name.

However as I now notice when the fn2 is empty, the parentheses around it should probably also not be there (similar to how you write the function type "function taking no parameters and returning void" as void(), not void()().

The case in std::tr1::function is exactly that one: A function taking no parameters and returning void, with extra parentheses around the empty "function name".

OK, now tested it: gdb indeed outputs void() as void()(); this probably should be considered a gdb bug.

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

std::tr1::_Bind<void (*(std::tr1::reference_wrapper<int>))(int&)>
like image 170
celtschk Avatar answered Oct 15 '22 00:10

celtschk