I cannot compile my program with VS2015 due to a function using a templated parameter and inheritance.
The error is this one.
I'm trying to achieve the following :
class A
{
//do something
};
class B : public A
{
//do something
};
template <typename T>
class Foo {
template <typename T>
friend void function(Foo<T> & sm) {
//do something
}
};
void main()
{
Foo<A> test;
Foo<B> test2;
};
I do understand the meaning of the error, but I do not understand why it actually happens.
I suppose function
is created with two different signatures :
void function(Foo<A> & sm);
and
void function(Foo<B> & sm);
How is that a multi-definition?
EDIT - The full error message :
Error C2995 'void function(Foo<T> &)': function template has already been defined
Both Clang and MS have the same complaint. Remove the second template specifier and it will compile.
class A{};
class B : public A{};
template <typename T>
class Foo {
// template <typename T>
friend void function(Foo<T> & sm) {
}
};
int main()
{
Foo<A> test;
Foo<B> test2;
};
T
is already specified for the class Foo
so its friend function is covered. You would us a second template
if there were a difference at the function, like:
class A{};
class B : public A{};
template <typename T>
class Foo {
template <typename U>
friend void function(Foo<T> & sm, U another) {
}
};
int main()
{
Foo<A> test;
Foo<B> test2;
};