c++ - 提供通用枚举类型作为模板参数

标签 c++ templates generics enums concept

简而言之:

有没有办法我可以喂 General模板类只代表一个 enum类型?像这样的东西:

template <typename T> struct General {};
struct EnumSpecific : General<any_enum_type> {};

<int>在我的情况下太多/不起作用。


我的具体情况:

  • 模板化 Holder类以通用方式处理任何类型的数据。
  • 摘要 General类实现依赖于 Holder 的特定算法s 的行为。
  • General 的模板规范(如 IntSpecificDoubleSpecificStringSpecificMoreSophisticatedTypeSpecific ..)定义如何处理一些具体的 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 .作为示例,这是一个将枚举类型作为模板参数的类。

#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++ - 提供通用枚举类型作为模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32316875/

相关文章:

c++ - 类内模板化类的 Typedef

java - 使用封闭的通用类的方法返回类型?

java - 快速遗留到泛型的转换问题

c++ - OpenMP 多线程建议

angularjs - Angular : difference when using template or templateUrl

c++ - 从模板参数派生并调用其复制构造函数

java - 层次结构和泛型

c++ - 将 QString 转换为 float 给我错误

c++ - 在Linux上使用Makefile编译独立的ASIO

c++ - 谁复制函数的返回值?