作为模板参数提供一个通用的枚举类型

11

简而言之:

我能否以某种方式使用仅表示枚举类型的内容来填充一个General模板类? 类似于:

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

<int>在我的情况下过于复杂/不起作用。


我的具体情况:

  • 一个模板化的Holder类以通用的方式处理任何类型的数据。
  • 一个抽象的General类依赖于Holder的行为实现特定算法。
  • 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 概念和更复杂的通用编程工具?


3
C++11 提供了 std::is_enum:http://en.cppreference.com/w/cpp/types/is_enum - Alan Stokes
@AlanStokes 他真是太好了!但这不能独立地传递给模板参数,对吧?这怎么能帮助我执行强制类型转换呢? - iago-lito
1
它可以与 enable_if 有益地使用。而且 underlying_type 可能有助于您的转换,尽管我承认在那个阶段我有点迷失在您的代码中了。 - Alan Stokes
1个回答

8
我们可以通过使用 std::enable_ifstd::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
}

糟糕!我无法让它工作。#include <type_traits> 应该足以使用你的代码吗?gcc 报错 error: no type named ‘type’ in ‘struct std::enable_if<false, void>. - iago-lito
这就是你应该得到的错误。如果传递了 false 的 enable_if,那么 type typedef 就不存在了,从而创建了一个编译器错误。在错误后应该有一个注释,如:note: while substituting prior template arguments into non-type template parameter ... - NathanOliver
哇!当然。我刚才有点糊涂,因为clang将错误定位在template行上。这对我来说仍然是魔法:你知道有没有一份好的C++通用编程教程吗?比template <typename T>更深入,并涵盖那些新的C++11神奇的东西? - iago-lito
1
这里有一些好书:https://dev59.com/_3RC5IYBdhLWcg3wK9yV - NathanOliver
太好了!我稍微尝试了一下,这确实可行!特质很棒。干杯! - iago-lito

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接