给出一个泛型枚举类型作为模板参数
Give a generic enum type as template argument
总之:
是否有一种方法可以为General
模板化类提供仅代表enum
类型的东西?比如:
template <typename T> struct General {};
struct EnumSpecific : General<any_enum_type> {};
<int>
太多/在我的情况下不起作用。
我的具体情况:
- 模板化的
Holder
类以通用方式处理任何类型的数据。 - 一个抽象的
General
类实现了依赖于Holder
行为的特定算法。 -
General
的模板规范(如IntSpecific
,DoubleSpecific
,StringSpecific
,MoreSophisticatedTypeSpecific
..)定义了如何处理一些具体的Holder
类型。 - 如何正确定义
EnumSpecific
规范?
下面是使我的问题发生的编译代码:
// A templated value holder:
template <typename T>
class Holder {
public:
Holder(T const& t) : _value(t) {};
// generic methods
void generics() {};
// methods concerning the value:
void set(T const& t /*, setInfo */) {
// .. check for an actual change, notify buddies of the change..
_value = t;
};
T value(/*readInfo*/) {
// .. do stuff depending on how / why the value is read..
return _value;
};
private:
T _value;
};
// (in reality, all `generics` methods come from a parent, untemplated class)
// A generic process involving such `Holder`s:
template <typename T>
class General {
public:
typedef bool /* or anything */ KnownReturnTypes;
General(Holder<T>* const a
, Holder<T>* const b)
: _a(a)
, _b(b)
{};
void methods() {
// Use common behavior of all `Holder`'s
_a->generics();
// .. or methods that rely on the actual values:
KnownReturnTypes knr( valuedMethods() );
if (knr) {} else {}
// ...
};
// Use polymorphism to adapt to each situation..
virtual KnownReturnTypes valuedMethods() = 0;
protected:
Holder<T>* _a;
Holder<T>* _b;
};
// Example of specialization for integral types (there might be others)
class IntSpecific : General<int> {
public:
IntSpecific(Holder<int>* const a
, Holder<int>* const b)
: General<int>(a, b)
{};
// implement the valuedMethods:
virtual KnownReturnTypes valuedMethods() {
return _a->value() > _b->value(); // dummy
}
};
// Specialization for enum types:
// * * * class EnumSpecific : General<any_enum_type> { // does not exist * *
class EnumSpecific : General<int> {
public:
EnumSpecific( Holder<int>* const a
, Holder<int>* const b)
: General<int>(a, b)
{};
// only use properties and methods offered by an enum type:
virtual KnownReturnTypes valuedMethods() {
return _a->value() == _b->value(); // dummy
}
};
// One particular case
typedef enum {One, Two, Three} Enum;
typedef Holder<Enum> EnumHolder;
int main() {
// Check that `IntSpecific` works fine.
Holder<int>* i( new Holder<int>(3) );
Holder<int>* j( new Holder<int>(5) );
IntSpecific is(i, j); // ok.
// Try the `EnumSpecific`
EnumHolder* a( new EnumHolder { One } );
EnumHolder* b( new EnumHolder { Two } );
EnumSpecific es(static_cast<Holder<int>*>(a) // invalid cast
, static_cast<Holder<Enum>*>(b)); // unexpected type
// This is because the compiler doesn't know enough about what
// EnumSpecific actually *is*. How to tell him more about it?
return EXIT_SUCCESS;
}
我应该在EnumSpecific : General<??>
中为模板参数提供什么以使编译器清楚?
我需要使用某种enum_type
概念和更复杂的泛型编程工具吗?
我们可以用std::enable_if
和std::is_enum
来完成这一点。作为示例,这是一个将enum类型作为模板形参的类。
#include <type_traits>
enum Enum { FOO, BAR};
template<typename T, typename std::enable_if<std::is_enum<T>::value>::type* = nullptr>
class Test {};
int main()
{
Test<Enum> a; // ok
Test<double> b; // error
}
相关文章:
- 在 c++ 中的模板实例化中使用带有构造函数的类作为类型参数
- 如何解决一元"*"(有"字符")错误的无效类型参数?
- "std::shared_ptr":不是参数"_Ty"的有效模板类型参数
- 具有可变参数非类型参数的模板专用化
- 函数类型参数的模板参数推导
- PowerShell 使用结构类型参数调用 C++ DLL 的导出函数
- 对于非常量指针类型的参数,未调用具有常量指针模板类型参数的功能
- 为模板传递非类型参数 agument
- 为什么带有类型参数的运算符 () 可以应用于 result_of 上下文中的类型?
- 使用其他模板类型参数作为要在函数签名中使用的类型别名声明
- 如何避免具有相同类型参数的函数中的错误
- 将内置类型变量传递给只有一个类类型参数的"+"运算符函数时自动类型转换的构造函数
- c++非类型参数包扩展
- 如何实现对参数顺序不可知的std::same_as的广义形式(即对于两个以上的类型参数)
- 在不同的模板参数包之间分发非类型参数包
- 如何在使用容器和字符串时强制使用显式分配器类型参数
- 错误:一元"*"的类型参数无效(具有"int"):使用 mergesort 计算
- EXPECT_CALL具有 unique_ptr 引用类型参数的模拟函数
- 作为模板类型参数,为什么 type[N] 与其专用版本不匹配----模板<类 T>类 S<T[]>
- C++ 模板:重载时找不到基类类型参数方法