【发布时间】:2019-06-07 16:07:26
【问题描述】:
我正在尝试做这样的事情(在 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> {
^~~
如何在 base 中为 bar::foo 的返回类型声明别名?不可能吗?
这个问题似乎相当相关:Special behavior for decltype of call operator for incomplete types,尽管我无法将那里给出的答案应用于我的案例。
【问题讨论】:
标签: c++ c++11 decltype crtp declval