【发布时间】:2020-12-11 07:54:53
【问题描述】:
我有以下代码,到目前为止,我们有衣服和鞋类。但是将来可能会有更多的项目对象类型。 Foo 类是用 Object 模板化的,这里的成员函数对于 eClothes 和 eFootwear 是相同的,但在实际代码中会进一步模板化。
有没有办法对方法 order 的实现进行重复数据删除?由于存在一对一的映射
- Object::clothes -> eClothes
- Object::footwear -> eFootwear
.. 有没有什么技术可以用来修复基于type 的order 的模板参数?这样一个类实例化只接受其对应类型的命令,否则它是一个编译时错误?
#include<iostream>
enum class Object{
clothes,
footwear,
};
enum class eClothes{
shirt,
};
enum class eFootwear{
flipflop,
};
template <Object type>
class Foo{
public:
template<eClothes clothkind>
void order(){
std::cout << "hey\n";
}
template<eFootwear footwearkind>
void order(){
std::cout << "hey\n";
}
};
int main(){
Foo<Object::clothes> foo_clothes;
Foo<Object::footwear> foo_footwear;
foo_clothes.order<eClothes::shirt>();
foo_footwear.order<eFootwear::flipflop>();
}
【问题讨论】:
标签: c++ templates enums c++17 sfinae