4

I can't seem to find the right syntax to specialize this template :

template <class Object, class Var, class Invert, class Step = Var, unsigned int FIXED = IW_GEOM_POINT>
class TSin : public BasicTween<Object, Var> {...

I want to keep <Object> as a template parameter but specialize all other parameters. I am trying it like this :

    template <class Object>
class TSin<Object, CIwVec2, int, CIwVec2, IW_GEOM_POINT> {...

This gives errors.

Please can someone provide the right syntax to specialize the template and the syntax to instantiate the specialized version?

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
Bill Kotsias
  • 3,258
  • 6
  • 33
  • 60
  • What's your compiler? Also, make a minimal complete example that reproduces the error. Your description and syntax look fine so far – Armen Tsirunyan Jul 18 '11 at 13:02
  • 1
    Your code as it stands (for the partial specialization) is fine. Most likely the problem is happening where you are trying to use this specialization - may be post a snippet of that? – Nim Jul 18 '11 at 13:03
  • You need to provide the error, your code looks fine. – Puppy Jul 18 '11 at 13:06
  • You were right. I had problem with make an instant of the specialization. The most elegant solution was to make a subclass of the initialization. – Bill Kotsias Jul 18 '11 at 13:37

2 Answers2

1

I think your code should look like here: http://ideone.com/cvGy3

You need to define all types for class instantiation.

Mazurov
  • 78
  • 5
0

The error is you're redefining class TSin. I don't think you can do that.

What you can do is declare the generic template and specialize the definitions of the class:

template <class Object, class Var, class Invert, class Step = Var, 
    unsigned int FIXED = IW_GEOM_POINT>
    class TSin;
template <class Object>
    class TSin<Object, CIwVec2, int, CIwVec2, IW_GEOM_POINT> {...

or specialize the definitions of members of the class:

template <class Object>
void TSin<Object, CIwVec2, int, CIwVec2, IW_GEOM_POINT>::Foo(...) {...

or declare a subclass:

template <class Object, class Var, class Invert, class Step = Var, 
    unsigned int FIXED = IW_GEOM_POINT>
    class TSin : public BasicTween<Object, Var> {...
template <class Object>
    class SpecialTSin::public TSin<Object, CIwVec2, int, CIwVec2, IW_GEOM_POINT>
{...

I think the latter option is the best.

Mike DeSimone
  • 41,631
  • 10
  • 72
  • 96
  • He isn't redefining TSin. He's doing a partial specialization (your option 1) – Armen Tsirunyan Jul 18 '11 at 13:03
  • You absolutely can do that. The original definition serves as an unspecialized base case. – Puppy Jul 18 '11 at 13:04
  • @DeadMG: How could that work, though? If a given set of template parameters could resolve to a base *definition* or a specialized *definition*, which gets used? What if there are overlapping specializations? What if a specialization is visible to one compilation unit and not others? It just looks like a recipe for pain. – Mike DeSimone Jul 19 '11 at 02:02
  • Method 2 is wrong - you can't partially specialize the definitions of members of the class (it will cause compilation error). – avtomaton Dec 12 '14 at 09:45
  • Even if you declare the member function as a template itself? – Mike DeSimone Dec 12 '14 at 13:52