用于检测非继承typedef的特性
Trait to detect non-inherited typedef
如果struct
具有未继承的typedef
,是否有方法进行检测?
以下代码在C
上失败
#include <iostream>
struct A { };
struct B : public A { typedef A Base; };
struct C : public B {};
template<typename T>
struct to_void
{
typedef void type;
};
template <typename T, typename dummy = void>
struct has_base_typedef : std::false_type {};
template <typename T>
struct has_base_typedef<T, typename to_void<typename T::Base>::type> : std::true_type {};
int main()
{
std::cout << has_base_typedef<A>::value;
std::cout << has_base_typedef<B>::value;
std::cout << has_base_typedef<C>::value;
}
C
的特征给出true
,因为Base
是遗传的(private
修饰语对AFAIK没有帮助)
我的目标是让代码段打印010
在ideone上玩的小例子。
基本上,您想要阻止Base
别名到派生类的隐式传播,我认为这是不可能的。我能想到的实现接近你想要的东西的唯一方法是:
struct C;
struct A {};
struct B : public A {
using Base = A;
using Derived = C;
};
struct C : public B { };
template<typename T>
struct to_void {
typedef void type;
};
template <typename T, typename dummy = void>
struct has_base_typedef : std::false_type {};
template <typename T>
struct has_base_typedef<T, std::enable_if_t<!std::is_same<typename T::Derived, T>::value,
typename to_void<typename T::Base>::type>> : std::true_type {};
实时演示
相关文章:
- C++:TypeDef使用元组
- 继承函数的重载解析
- 继承期间显示未知行为的子类
- 头文件-继承c++
- 为什么在保护模式下继承升级不起作用
- 通过继承类使用来自不同命名空间的运算符
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 混合组合和继承的C++问题
- 继承:构造函数,初始化C++11中基类的类C数组成员
- 从类继承时,继承的类是否会通过父类重新定义继承的变量
- 有没有一种方法可以通过"typedef"为重新定义的基本类型定义特征和强制转换运算符
- 公共与私人继承
- 为什么当一个模板类继承自另一个模板类时,需要重新指定 typedef 并限定函数调用?
- 是无限的Typedef函数指针继承
- 'typedef' C++类中自动继承吗?
- 基类中的typedef在继承类中可见,而不是完全限定的
- 如何从通过模板继承的类中提取 typedef'd 信息?
- C++名称与typedef别名和继承的名称冲突
- 用于检测非继承typedef的特性
- typedef与继承中typename的用法