【发布时间】:2012-03-11 06:12:44
【问题描述】:
为什么一个类不能在继承列表中有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)。
【问题讨论】:
-
你试过 gcc 4.7、clang 或任何其他编译器吗? gcc 还不完全符合 C++11...
-
@PlasmaHH 我现在实际上正在尝试编译 clang(出于其他原因),但对于 MSVC++ 2010,它不起作用。
标签: c++ inheritance c++11 decltype