unspecified bind

307 views Asked by At

I was researching what std::bind is and what it's for (that may eventually be a different question) at MSDN: http://msdn.microsoft.com/en-us/library/bb982702.aspx

And saw that the prototypes listed are:

template<class Fty, class T1, class T2, ..., class TN>
   unspecified bind(Fty fn, T1 t1, T2 t2, ..., TN tN);
template<class Ret, class Fty, class T1, class T2, ..., class TN>
   unspecified bind(Fty fn, T1 t1, T2 t2, ..., TN tN);

Which confuses me for two reasons. 1) Last I checked, MSVC didn't implement variadic templates, and 2) My question: what does the word unspecified mean there? (It doesn't seem related to unspecified behavior.)

3

There are 3 answers

1
David Rodríguez - dribeas On BEST ANSWER

The unspecified in the return type means that it is a type that is, well, unspecified. The standard does not require a particular type to be returned from bind, as long as the returned type complies with the requirements that the standard mandates.

As of the ..., I don't know whether VS implements them or not, but you will see the same documentation for boost::bind, and it has been compiling in different compilers without variadic template support for some time already... the documentation states that you can pass N arguments of different types, but that does not necessarily mean that there is a single template that does it, it can be implemented in terms of a set of 1-ary, 2-ary... N-ary templates.

The documentation is showing the behavior that you can expect from using it, rather than the detail of how it is implemented.

0
Rob Kennedy On

There are separate overloads that expect {1, 2, ..., N} arguments. Rather than list them all, the documentation uses shorthand to show that you can use various numbers of parameters. It's not meant to illustrate valid C++ syntax. I would expect the documentation to mention somewhere what N is.

The result type depends on the arguments passed in. It will be some form of functor, and that functor will generally be storable in a function object, or you can pass the result directly to something like for_each and let the compiler's template type deduction figure it out. As long as the result is callable, the actual type doesn't matter, and would be complicated to try to explain briefly in documentation.

0
Nicol Bolas On

Visual Studio doesn't implement variadic templates. They emulate the concept by essentially having lots of overloads of the function (likely built via macros). This is how boost::bind is implemented, which is where std::bind comes from.

The unspecified return type of std::bind is exactly that. It is required to be an object which is of a moveable type. That object will overload operator() so that the function bound by std::bind can be called.