给出一个通用枚举类型作为模板参数

2024-03-04

简而言之:

有什么方法可以喂养General模板化类仅代表一个enum类型?就像是:

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

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


我的具体案例:

  • 一个模板化的Holder类以通用方式处理任何类型的数据。
  • 摘要General类实现特定的算法依赖于Holder的行为。
  • 模板规格General (like IntSpecific, DoubleSpecific, StringSpecific, MoreSophisticatedTypeSpecific..) 定义如何处理一些具体的Holder types.
  • 我怎样才能正确定义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 http://en.cppreference.com/w/cpp/types/enable_if and std::is_enum http://en.cppreference.com/w/cpp/types/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
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

给出一个通用枚举类型作为模板参数 的相关文章

随机推荐