I've encountered a compiler error using Visual Studio 2010 which I've reduced down to the following code:
template <int i> struct A
{
typedef int T;
};
template<int i>
struct B
{
static const int i = i; // <-- this seems to cause the problem
typename A<i>::T F();
};
template<int i>
typename A<i>::T B<i>::F() { return B<i>::i; }
This code produces this error:
repro.cpp(15): error C2244: 'B<i>::F' : unable to match function definition to an existing declaration
repro.cpp(12) : see declaration of 'B<i>::F'
definition
'A<i>::T B<i>::F(void)'
existing declarations
'A<i>::T B<i>::F(void)'
If the declaration for i
in struct B
is removed the compiler error goes away. I believe it's because the template parameter for the return type of F
is binding to the static member i
within B
instead of B
's template argument. Why do the return types for F
'differ' when the v开发者_JAVA百科alue for i
is the same? Is this a bug?
I should also mention that if the function is declared inline the error goes away.
The problem is you are declaring the same name twice in the same scope. If you rename the static const int i, or the template parameter it should work.
精彩评论