为什么一个类不能在继承列表中使用
decltype
?例如,我期望以下代码会使得 A<B>
继承自 RType
,但在使用 G++ 4.6.1(使用 -std=c++0x
)编译时,它并没有通过编译:#include <type_traits>
template<typename T>
class A : public decltype(std::declval<T>().hello()) { };
class RType { };
class B {
public:
RType hello() { return RType(); }
};
int main() {
A<B> a;
}
它会产生以下输出:
test.cpp:6:18: error: expected class-name before 'decltype'
test.cpp:6:18: error: expected '{' before 'decltype'
test.cpp:6:54: error: expected unqualified-id before '{' token
test.cpp: In function 'int main()':
test.cpp:16:7: error: aggregate 'A<B> a' has incomplete type and cannot be defined
使用declval
的目的只是为了提供一个需要使用decltype
的示例,但是其他用途的decltype
也会失败(即在没有declval
的情况下)。