19

I found out that its not trivial to have a neat syntax like in:

std::function<int(float, bool)>

If I declare the function as:

template <class RetType, class... Args>
class function {};

It would be an ordinary syntax to define function's templated types:

function<int,float,bool> f;

But it works with strange trick with partial template specialization

template <class> class function; // #1

template <class RV, class... Args>
class function<RV(Args...)> {} // #2

Why is that? Why I need to give the template an empty general form with empty type parameter (#1) or otherwise it just won't compile

0

4 Answers 4

9

This is just the way the language was designed. Primary templates can't do complex decomposition of types like that; you need to use partial specialization.

If I understand correctly, you would like to just write the second version without having to supply the primary template. But think about how the template arguments map to the template parameters:

template <class RV, class Arg1, class... Args>
class function<RV(Arg1, Args...)> {}

function<int(float,bool)>; //1
function<int, float, bool>; //2

Option 1 is what you want to write, but note that you pass a single function type to a template where the parameters are a two type parameters and a type parameter pack. In other words, writing this without a primary template means that your template arguments wouldn't necessarily match the template parameters. Option 2 matches the template parameters, but it doesn't match the specialization.

This makes even less sense if you have more than one specialization:

template <class RV, class Arg1, class... Args>
class function<RV(Arg1, Args...)> {}

template <class T, class RV, class Arg1, class... Args>
class function<RV (T::*) (Arg1, Args...)> {}

You could maybe think up some rules to infer the primary template from the specializations, but that seems pretty awful to me.

2
  • I see... so the template parameters are just named placeholders for a types (or type parameter packs, or whatever). But it should match the actual usage / specialization that s where the actual type is emerging, right?
    – barney
    Aug 18, 2016 at 13:01
  • 1
    @barney Yes, the template parameters are essentially placeholders. The used specialization needs to match the template parameter list for the primary template. Aug 18, 2016 at 13:06
9

You have to keep in mind that int (float, bool) is a type. More precisely, it's the type "function which takes two parameters of type float and bool, and returns int."

Since it's a type, it's obvious the template must have one type parameter in the place where you want to use the syntax int (float, bool).

At the same time, having just the function type is unwieldy. Sure, you could do it easily. For example, if all you need to do is some kind of a forwarder:

template <class T>
struct CallForwarder
{
  std::function<T> forward(std::function<T> f)
  {
    std::cout << "Forwarding!\n";
    return f;
  }
};

However, as soon as you want access to the "components" of the function type, you need a way to introduce identifiers for them. The most natural way to do so is the partial specialisation for function types, just as you did in your question (and just as std::function does).

2
  • Yes, it does make sense. So it is allowed for partial specialization to SEPARATE type's internal components naming them with identifiers by introducing them in "template <type1 id1, type2 id ...>" list, right? So basically this list is NOT the list of actual types that are being used, i.e. if I declare template <class T> but use in actual function parameters list "template <class T> void process(std::function<T> f);" the type of template would be process_mangled_name__function_from_t, not T itself right?
    – barney
    Aug 18, 2016 at 12:59
  • 1
    @barney For the first question, yes. Partial specialisations allow you to provide an alternative definition of the template, used when the arguments conform to a certain pattern (such as a function type in our case). The second question I can't quite understand, though :-( Aug 18, 2016 at 13:03
7

You can actually do this:

template <typename T>
class X { };

X<int(char)> x;

And inside the definition of X you could create a std::function<T> as you would create a std::function<int(char)>. The problem here is that you cannot (at least easily) access the return type and the type of the parameters of the argument (int and char here).

Using the "trick", you can access these without problem - It "simply" makes your code cleaner.

4

"Why I need to give the template an empty general form with empty type parameter"

Because, you want explicit type differentiation among the "return type" and the "argument types", with the similar lucidness of syntactic sugar. In C++, no such syntax is available for the first hand version of the template class. You must have to specialize it, to be able to distinguish it.

After reading your Q, I looked at the <functional> header file. Even std::function does the same trick:

// <functional>  (header file taken from g++ Ubuntu)

template<typename _Signature>
class function;

// ...

 /**
   *  @brief Primary class template for std::function.
   *  @ingroup functors
   *
   *  Polymorphic function wrapper.
   */
template<typename _Res, typename... _ArgTypes>
class function<_Res(_ArgTypes...)>

As you see in their comments, they treat the specialized version as the "Primary" class. Hence this trick is coming from the standard header itself!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.