Would someone kindly tell me why the complier flags an error when I attempt to call the versions of the member function Foo<T>::setValue that involve more than 1 template argument, as shown below.
See ideone
class Bar
{
public:
enum TYPE{};
};
//////////////////////////////////////////////////////////////////////////////////////
template<typename T>
class Foo
{
public:
template<typename P>
void setValue1();
template<typename P, int>
void setValue2();
template<typename P, typename P::TYPE>
void setValue3();
private:
T m_value;
};
//////////////////////////////////////////////////////////////////////////////////////
template<typename T>
template<typename P>
void Foo<T>::setValue1()
{
}
template<typename T>
template<typename P, int>
void Foo<T>::setValue2()
{
}
template<typename T>
template<typename P, typename P::TYPE>
void Foo<T>::setValue3()
{
}
//////////////////////////////////////////////////////////////////////////////////////
int main()
{
Foo<Bar::TYPE> f1;
f1.setValue1<Bar>(); // Compiles
f1.setValue2<Bar, int>(); // ERROR
f1.setValue3<Bar, Bar::TYPE>(); // ERROR
return EXIT_SUCCESS;
}
GCC error:
error: no matching function for call to ‘Foo<Bar::TYPE>::setValue2()’
error: no matching function for call to ‘Foo<Bar::TYPE>::setValue3()’
MSVC .NET 2008 error:
Test6.cpp(60) : error C2975: 'Foo<T>::setValue2' : invalid template argument for 'unnamed-parameter', expected compile-time constant expression
with
[
T=Bar::TYPE
]
Test6.cpp(24) : see declaration of 'Foo<T>::setValue2'
with
[
T=Bar::TYPE
]
Test6.cpp(61) : error C2975: 'Foo<T>::setValue3' : invalid template argument for 'unnamed-parameter', expected compile-time constant expression
with
[
T=Bar::TYPE
]
Test6.cpp(27) : see declaration of 'Foo<T>::setValue3'
with
[
T=Bar::TYPE
]
I addition to the problem @Jason mentioned, your second function template takes an
intas its second argument, and you are providing a type.This was fixed by changing the name to
setValue2per @Jason’s post, then changing the function call to:I’m not sure what you’re trying to accomplish with the third template function definition. It seems you should only need one template argument, since P::Type can also be derived.
I should also note that your class definition is not really precise — the class’s template argument is a
typename, but you’re passing itBar::TYPEwhich is anenumtype.