template <class T>
struct foo {
int x;
decltype(x) f1();
};
Run Code Online (Sandbox Code Playgroud)
似乎不可能定义f1外线.我尝试了以下定义,但它们都不起作用:
template <class T> decltype(x) foo<T>::f1() {}
template <class T> auto foo<T>::f1() -> decltype(x) {}
template <class T> auto foo<T>::f1() { return x; }
template <class T> decltype(std::declval<foo<T>>().x) foo<T>::f1() {}
// This return type is copied from the gcc error message
template <class T> decltype (((foo<T>*)(void)0)->foo<T>::x) foo<T>::f1() {}
Run Code Online (Sandbox Code Playgroud)
这不是在实际代码中的问题,因为改变的一流声明f1,以auto f1() -> decltype(x);使第二个定义.但我很困惑为什么会改变什么呢.甚至可以宣布原始f1的外线?