tags:

views:

132

answers:

1
template<typename T1, typename T2>
class Bimap {
public:
    class Data {
    private:
        template<typename T> Data& set(T);
        template<> Data& set<T1>(typename T1 v) { /*...*/ }
    };
};

That gives me the error:

error: explicit specialization in non-namespace scope 'class Bimap<T1, T2>::Data'

I understand what the error is saying. But why I can't I do this? And how can I fix it?

+4  A: 

One way forget templates, overload:

Data& set(T1 v) { /*...*/ }

but here is a trick which I use sometimes

you can specialize class template within class:

class {
    template<typename T>
    struct function_ {
        static void apply(T);
    };

    template<>
    struct function_<int> {
        ...
    };

    template<typename T>
    void function(T t) { return function_<T>::apply(t); }
aaa
+1, yeah, the only sane way to get function template specializations right.
Alexandre C.
You *can* explicitly specialize a nested class template within an *ordinary* class. But you *cannot* explicitly specialize a nested class template within another *class template*. The only way to do the latter is to explicitly specialize *both* the enclosing template and the nested template.
AndreyT
@Andrey I was not sure about template class inside template class. at any rate, some variation of the trick can probably be made
aaa