【发布时间】:2012-09-11 15:58:10
【问题描述】:
我使用 decltype 作为成员函数的返回类型,但定义和声明不匹配。这是一些代码:
template<typename T>
struct A {
T x;
auto f() -> decltype(x);
};
template<typename T>
auto A<T>::f() -> decltype(x) {
return this->x;
}
int main() {}
这会产生
test.cc:10:6: error: prototype for 'decltype (((A<T>*)0)->A<T>::x) A<T>::f()' does not match any in class 'A<T>'
test.cc:6:7: error: candidate is: decltype (((A<T>*)this)->A<T>::x) A<T>::f()
区别在于定义有(A<T>*)0,而声明有(A<T>*)this。什么给了?
【问题讨论】:
-
A<T>::x而不仅仅是x说什么。不过,我不知道它是否有效,或者是否应该这样。 -
@ChristianRau 这确实有效,现在是我实施的解决方案,直到 gcc 修复此问题。