Question

My class declaration begins as follows:

template< 
    class P, 
    class B, 
    class comparePosition = compareHorizontal< P >
>
class BlahBlah  { ...

where compareHorizontal is a template function. When I attempt to compile, clang spits out

[...]/entity.hpp:57:33: error: type name requires a specifier or qualifier
        class comparePosition = compareHorizontal< P >
                                ^

[...]/entity.hpp:57:33: error: C++ requires a type specifier for all declarations
        class comparePosition = compareHorizontal< P >
                                ^~~~~~~~~~~~~~~~~

(and a whole lot of other errors on the same line).

If I just remove the default template parameter, leaving all else intact, it compiles just fine. So I wonder, how would I use a function template as a default argument, if that's even possible? Or would I be better off just making a functor class with an operator() that calls compareHorizontal and use that instead?

Was it helpful?

Solution

I think the reason is that the template function is not a type. It is a specific value if you think of it, the type of the function goes something like this:

  template<
       class P,
       class B,
       class C=bool (*)(P&)
   >
   class BlahBlah  {
   };

and it compiles. It is as if you said class C=5; this also will not compile, because 5 is not a type. I suggest you use a struct in such case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top