How to provide explicit specialization to only one method in a C ++ template class?

I have a template class that looks something like this:

template<class T> class C
{
    void A();
    void B();

    // Other stuff
};

template<class T> void C<T>::A() { /* something */ }
template<class T> void C<T>::B() { /* something */ }

I want to provide explicit specialization only for A, keeping the default value for Band "other stuff".

I tried so far

class D { };
template<> void C<D>::A() { /*...*/ } // Gives a link error: multiple definition

Every other option I tried to execute with parse errors.


What I've done:

The original problem was that the explicit specialization was in the header file, so it was dumped into several object files and ruined the link (why does the linker not notice that all instances of the symbol are the same, just close up?)

, . , . , GCC , .

+5
2

Martin York :

class D { };
template<> void C<D>::A(); // Don't implement here!

.cpp :

template<> void C<D>::A() { /* do code here */ }

, , . , .

+8

Try

template<> inline void c<int>::A() { ... }
//         ^^^^^^

. , , . . jsut .

+5

All Articles