为什么类不能从 decltype 的结果继承?
Why can't a class inherit from the result of a decltype?
为什么一个类在继承列表中不能有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
)。
允许:
10.1:"基类列表可以在类定义中使用以下符号指定:"
class-or-decltype:
nested-name-specifieropt class-name
decltype-specifier
所以我猜你的编译器有错误
它看起来像GCC中的一个bug。试试4.7。
解决方法:
template <typename T>
class ID
{
public:
typedef T type;
};
template<typename T>
class A : public ID<whatever>::type { };
您可以尝试类似的方法
template<typename T>
class A : public result_of<T::hello()>
尽管它可能期望该语法有一个静态成员函数,而且它可能会遇到阻止decltype工作的同一个错误。
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 继承函数的重载解析
- valgrind-hellgrind与泄漏检查的结果不同
- 用C++20 fmt限制结果的总大小
- 继承期间显示未知行为的子类
- 如何返回一个类的两个对象相加的结果
- 头文件-继承c++
- 为什么在保护模式下继承升级不起作用
- 通过继承类使用来自不同命名空间的运算符
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 使用QProcess执行命令,并将结果存储在QStringList中
- 继承和IS_DETECTED_V提供了一个奇怪的结果(C 17)
- 继承中出现奇怪的意外结果
- 为什么类不能从 decltype 的结果继承?
- 在C++中,当继承一个类并重写一个函数时.从数组中调用那个被重写的函数时,我得到了一个意外的结果
- 如何使从基类继承的对象访问字符方法,但有自己的结果
- 改变继承顺序会产生不同的结果
- 继承示例没有打印预期的结果
- 从空类继承时如何解释此 sizeof 结果
- c++虚拟公共继承初始化奇怪的结果