7

A template template specification is like this:

template < template < class > class T >
struct MyTemplate
{
};

How am I supposed to create a total (or partial) specialization for this template? Is this possible?

scooterman
  • 1,336
  • 2
  • 17
  • 36
  • 1
    ...Somewhere exciting! I can't wait! – John Dibling Aug 13 '10 at 16:46
  • Who in the world voted to close, and why? This seems to be a perfectly legitimate, topical question. – Jerry Coffin Aug 13 '10 at 16:51
  • @Jerry: If you saw the first 30 seconds of the question (where the vote was cast), it would make sense. It was just the code up to `{` with no question. I made the comment: "`}; // where are you going with this?`" to which @John replied. But it's fixed now. :) – GManNickG Aug 13 '10 at 16:54
  • @Jerry Somehow half of my question didn't showed up.. My mistake, sorry. – scooterman Aug 13 '10 at 16:57
  • @scooterman: It's all okay. It's a good question, too. – GManNickG Aug 13 '10 at 16:58
  • Am I the only one who finds it extremely confusing that some (early?) edits don't show up as edits? – jalf Aug 13 '10 at 19:37
  • @jalf: The rationale or the mechanism? – GManNickG Aug 13 '10 at 22:37
  • @GMan: the mechanism. I'm not too sure on the rationale either, but it gets confusing when you see (or write) comments in response to information that is no longer there, in a question with no edit history. – jalf Aug 13 '10 at 22:45
  • @jalf: Ah. It's the first 5 minutes of the question (and answers), after that edits show. I think the rationale is that the time span is enough to fix those minor formatting errors, grammatical errors, etc. – GManNickG Aug 14 '10 at 05:39

2 Answers2

5

Like this:

#include <iostream>

template <typename T>
struct foo{};

template <typename T>
struct bar{};

template < template < class > class T >
struct MyTemplate
{
    static const bool value = false;
};

template <>
struct MyTemplate<bar>
{
    static const bool value = true;
};


int main(void)
{
    std::cout << std::boolalpha;
    std::cout << MyTemplate<foo>::value << std::endl;
    std::cout << MyTemplate<bar>::value << std::endl;
}
GManNickG
  • 494,350
  • 52
  • 494
  • 543
  • GMan, there is a way to enforce on MyTemplate specialization what kind ot T bar should expect? – scooterman Aug 13 '10 at 17:02
  • @scooterman: Well in your class you'd just use `bar` and it'll work or not. If I'm understanding your question correctly. – GManNickG Aug 13 '10 at 17:35
3

A specialization of this would, for example, be:

template<>
struct MyTemplate<std::auto_ptr> {
   // ...
};
sth
  • 222,467
  • 53
  • 283
  • 367