TypeA和TypeB是同一个主类型的别名,如何在C++模板中将它们推导为不同的类型
TypeA and TypeB are alias to a same primary type, how can they be deduced as different type in C++ template?
我有两个不同的别名类型指向同一主类型:
using TypeA = unsigned long
using TypeB = unsigned long
我正在为他们实施类型特征。我的目标是为它们推断出不同的价值。
template <typename T>
struct LengthTrait {
constexpr static int size = 5;
};
template<>
struct LengthTrait<TypeA> {
constexpr static int size = 4;
};
template<>
struct LengthTrait<TypeB> {
constexpr static int size = 8;
};
但编译器告诉我TypeA和TypeB都是unsigned long
类型,所以代码无法编译。
我的问题是:有什么方法可以区别对待A型和B型吗?
我尝试使用typedef
而不是using
,但它不起作用。
就类型系统而言,别名并不存在,它们都是unsigned long
,其他什么都不是。
据我所知,最接近的事情是让它们成为枚举:
enum TypeA : unsigned long {};
enum TypeB : unsigned long {};
这样,它们是唯一的类型,仍然可以用作整数(有点烦人(。
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如何获取std::result_of函数的返回类型
- 从父命名空间重载类型
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 访问者访问变体并返回不同类型时出错
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- TypeA和TypeB是同一个主类型的别名,如何在C++模板中将它们推导为不同的类型
- 如何在同一个模板功能上专门化几种类型?
- 将多个类型存储到同一个容器中
- 在c++中,如何在另一个嵌套类中使用嵌套类类型(两个嵌套类在同一个外部类下)
- 同一个模板的多个专门化在单个类中具有不同的类型名
- 同一个类型限定符被多次使用