为什么一个类不能从decltype的结果继承?

Set*_*gie 13 c++ inheritance decltype c++11

为什么一个类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;
}
Run Code Online (Sandbox Code Playgroud)

它给出了以下输出:

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
Run Code Online (Sandbox Code Playgroud)

使用declval只是为了提供您需要使用的实例decltype,但其他用途decltype也会失败(即没有declval).

Mr.*_*bis 19

这是允许的:

10.1:"可以使用表示法在类定义中指定基类列表:"

class-or-decltype:
nested-name-specifieropt class-name
decltype-specifier
Run Code Online (Sandbox Code Playgroud)

所以我猜你的编译器有bug


Ale*_*x B 10

它看起来像是GCC中的一个错误.试试4.7.