What is the proper way to declare a specialization of a template for another template type?

Posted by Head Geek on Stack Overflow See other posts from Stack Overflow or by Head Geek
Published on 2010-04-28T02:10:06Z Indexed on 2010/04/28 2:13 UTC
Read the original article Hit count: 247

Filed under:
|

The usual definition for a specialization of a template function is something like this:

class Foo {
    [...]
};

namespace std {
    template<>
    void swap(Foo& left, Foo& right) {
        [...]
    }
} // namespace std

But how do you properly define the specialization when the type it's specialized on is itself a template? Here's what I've got:

template <size_t Bits>
class fixed {
    [...]
};

namespace std {
    template<size_t Bits>
    void swap(fixed<Bits>& left, fixed<Bits>& right) {
        [...]
    }
} // namespace std

Is this the right way to declare swap? It's supposed to be a specialization of the template function std::swap, but I can't tell whether the compiler is seeing it as such, or whether it thinks that it's an overload of it or something.

© Stack Overflow or respective owner

Related posts about c++

Related posts about templates