Quick question on template specializations

Dec 1, 2008 at 4:38am
Ok, if I have a template like this: template <class T, class Y> class myclass { ... };
And I have a template specialization like this: template <> class myclass <char> { ... };

How will the compiler know whether I'm referring to T or Y in the template specialization?
Dec 1, 2008 at 4:44am
Have you tried compiling it and seeing if it gives you any errors?

I don't think it would work though...unless you assigned a default type to Y.
Last edited on Dec 1, 2008 at 4:45am
Dec 1, 2008 at 4:50am
Hint: your code doesn't compile as is. The compiler error should be obvious to fix, and once you fix it, the answer to your question is obvious.
Dec 1, 2008 at 4:59am
Well, I learnt something new today. I will probably forget it in six or so hours due to limited application, though. Just like I did with ... and ... .
Dec 1, 2008 at 5:03am
"main.cpp:33: error: wrong number of template arguments (1, should be 2)"

Huh. So I have to explicitly define in the template specialization what every template parameter has to be. WHY didn't I think of that, it's blatantly obvious >_> . Thanks anyway :P
Dec 1, 2008 at 5:22am
You are only partially specializing. So you still have to leave the second parameter templated (or the first, whichever you want).

Topic archived. No new replies allowed.