我正在尝试做这样的事情(在 c++11 中):
#include <utility>
template <typename T>
struct base {
using type = decltype( std::declval<T>().foo() );
};
struct bar : base<bar> {
int foo() { return 42;}
};
int main() {
bar::type x;
}
失败了
prog.cc: In instantiation of 'struct base<bar>':
prog.cc:8:14: required from here
prog.cc:5:46: error: invalid use of incomplete type 'struct bar'
using type = decltype( std::declval<T>().foo() );
~~~~~~~~~~~~~~~~~~^~~
prog.cc:8:8: note: forward declaration of 'struct bar'
struct bar : base<bar> {
^~~
如何为bar::foo
in的返回类型声明别名base
?不可能吗?
这个问题似乎是相当相关的:对于不完整类型的呼叫运算符的 decltype 的特殊行为,尽管我无法将那里给出的答案应用于我的案例。