【发布时间】:2011-06-25 03:15:11
【问题描述】:
我在使用 Visual Studio 2010 时遇到了编译器错误,我已将其简化为以下代码:
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; }
此代码产生此错误:
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)'
如果结构B 中i 的声明被删除,编译器错误就会消失。我相信这是因为F 的返回类型的模板参数绑定到B 中的静态成员i,而不是B 的模板参数。当i 的值相同时,为什么F 的返回类型“不同”?这是一个错误吗?
我还应该提到,如果函数被声明为内联,错误就会消失。
【问题讨论】:
标签: c++ visual-studio templates compiler-errors