2

I have this hpp

namespace A
{
    template<class T>
    class MyC
    {
    public:
        T a;
    };

    template<class T>
    void F(T r);
}

and this cpp

template<>
void A::F<double>(double r)
{
    r;
}

template<>
void A::F<int>(int r)
{
    r;
}

template<class T>
void A::F<A::MyC<T>>(A::MyC<T> r)
{
    r;
}
template void A::F<A::MyC<int>>(A::MyC<int>);
template void A::F<A::MyC<double>>(A::MyC<double>);

but compiler says "unable to match function definition to an existing declaration" about F.

What's wrong with this code?

sepp2k
  • 363,768
  • 54
  • 674
  • 675
brachistochron
  • 321
  • 1
  • 11
  • on `A::F>` right - the others are ok? – doctorlove Dec 13 '13 at 10:47
  • yep, other overloads are okay. bad things happens only when we using template type like parameter for template function – brachistochron Dec 13 '13 at 11:49
  • You're not overloading in the cpp, but explicitly specializing and explicitly instantiating. `F`, being a function template, *is* a set of overloaded functions. – dyp Dec 13 '13 at 12:25
  • "Wrong" is that partial template specialization of a function is prohibited. F should be a class/struct for this to work. And yes, this is not overload but template specialization. You may want to look at [this question](http://stackoverflow.com/questions/8061456/c-function-template-partial-specialization). – Abstraction Dec 14 '13 at 09:36

1 Answers1

2

Put those declarations all in namespace A { ... } and remove A::. On the other hand, function template partial specialization is not allowed and this will make error:

template<class T>
void F<MyC<T>>(MyC<T> r)
{
    ...
}
masoud
  • 55,379
  • 16
  • 141
  • 208