C ++和typetraits:定义可能定义列表的最简单方法

时间:2012-03-05 17:19:29

标签: c++ templates typetraits

我想定义一个函数template<typename T> T constCast(const ScriptVar_t& s);。根据{{​​1}},我希望有不同的定义。 (T是一个类,但在此上下文中,细节并不重要。)

ScriptVar_t上的条件不像特定类型那么简单,它们都是一些更复杂的静态布尔表达式。即我有一个表达式列表T .. ext1,对于每个表达式,我都有一个函数的定义。我希望按顺序检查它们,并且应该使用第一个匹配表达式的定义。如果所有这些都失败了,我想得到一个编译器错误。

现在,我只有2个定义,我的代码看起来像这样(这是一个完整的测试用例,相关代码已标记):

extN

经过几次尝试后我想出了这个,直到它有效。

(从代码中可以看出,这两个表达式是#include <boost/type_traits.hpp> enum { SVT_INT, SVT_FLOAT, SVT_BASEOBJ, SVT_CUSTOMVAR }; struct BaseObject {}; struct CustomVar {}; template<typename T> struct GetType; template<> struct GetType<int> { static const int value = SVT_INT; }; template<> struct GetType<float> { static const int value = SVT_FLOAT; }; template<> struct GetType<BaseObject> { static const int value = SVT_BASEOBJ; }; template<bool> struct GetType_BaseCustomVar; template<> struct GetType_BaseCustomVar<true> { struct Type { static const int value = SVT_CUSTOMVAR; }; }; template<typename T> struct GetType : GetType_BaseCustomVar<boost::is_base_of<CustomVar,T>::value>::Type {}; struct ScriptVar_t; template<typename T> T CastScriptVarConst(const ScriptVar_t& s); struct ScriptVar_t { operator int() const { return 0; } operator float() const { return 0.0f; } operator BaseObject() const { return BaseObject(); } template<typename T> T* as() const { return NULL; } template <typename T> T castConst() const { return CastScriptVarConst<T>(*this); } }; // *** relevant code starts here template<typename T> T CastScriptVarConst(const ScriptVar_t& s); template<bool> struct CastScriptVar1; template<typename T> struct CastScriptVar1_IsSimpleType { static const bool value = GetType<T>::value < SVT_BASEOBJ; }; template<> struct CastScriptVar1<true> { template<typename T> static T castConst(const ScriptVar_t& s, const T& /*dummy*/) { return (T) s; } }; template<bool> struct CastScriptVar2; template<typename T> struct CastScriptVar2_IsCustomVar { static const bool value = boost::is_base_of<CustomVar,T>::value; }; template<> struct CastScriptVar2<true> { template<typename T> static T castConst(const ScriptVar_t& s, const T& /*dummy*/) { return *s.as<T>(); } }; template<> struct CastScriptVar1<false> { template<typename T> static T castConst(const ScriptVar_t& s, const T& /*dummy*/) { return CastScriptVar2<CastScriptVar2_IsCustomVar<T>::value>::castConst(s, T()); } }; template<typename T> T CastScriptVarConst(const ScriptVar_t& s) { return CastScriptVar1<CastScriptVar1_IsSimpleType<T>::value>::castConst(s, T()); } int main() { ScriptVar_t v; v.castConst<int>(); v.castConst<CustomVar>(); } GetType<T>::value < SVT_BASEOBJ。如果两者都是false,编译器应该抛出错误。但这只是我的问题的一个例子。 )

我想知道这段代码是否有更简洁的解决方案。


作为参考,我正在玩它here。而现在,我再次对所有其他解决方案提出了一些不同的解决方案。

3 个答案:

答案 0 :(得分:2)

所以,如果我理解正确,你有两种方法。如果GetType<T>::value < SVT_BASEOBJ,那么您只想使用普通演员:(T) s;

另一方面,如果GetType<T>::value < SVT_BASEOBJ为false,那么您需要确保CustomVarT类型的基类(即T派生自CustomVar {1}})。然后,您想在该对象上使用成员函数:*s.as<T>()

否则您需要编译错误。

以下是使用重载和std::enable_if

执行此操作的一种方法
template<typename T>
typename std::enable_if<GetType<T>::value < SVT_BASEOBJ,T>::type
CastScriptVarConst(const ScriptVar_t& s) {
    return (T) s;
}

template<typename T>
typename std::enable_if<!(GetType<T>::value < SVT_BASEOBJ)
                        && std::is_base_of<CustomVar,T>::value,T>::type
CastScriptVarConst(const ScriptVar_t& s) {
    return *s.as<T>();
}

enable_if利用C ++中的SFINAE规则,这样如果条件失败,唯一的结果就是该函数没有被添加到该调用的可行重载集合中。由于enable_if条件最多是相互排斥的,因此对于任何给定的调用,函数都将是可行的,因此永远不会有歧义。如果这两个条件都不成立,那么你会得到一个编译错误,说它无法找到匹配的函数。


#include <type_traits>
#include <iostream>

enum {
    SVT_INT,
    SVT_FLOAT,
    SVT_BASEOBJ,
    SVT_CUSTOMVAR
};

struct BaseObject {};
struct CustomVar {};

template<typename T> struct GetType;
template<> struct GetType<int> { static const int value = SVT_INT; };
template<> struct GetType<float> { static const int value = SVT_FLOAT; };
template<> struct GetType<BaseObject> { static const int value = SVT_BASEOBJ; };

template<bool> struct GetType_BaseCustomVar;
template<> struct GetType_BaseCustomVar<true> {
    struct Type { static const int value = SVT_CUSTOMVAR; };
};
template<typename T> struct GetType : GetType_BaseCustomVar<std::is_base_of<CustomVar,T>::value>::Type {};

struct ScriptVar_t {
    operator int() const { return 0; }
    operator float() const { return 0.0f; }
    operator BaseObject() const { return BaseObject(); }
    template<typename T> T* as() const { return NULL; }

    template <typename T> T castConst() const;
};

template<typename T>
typename std::enable_if<GetType<T>::value < SVT_BASEOBJ,T>::type
CastScriptVarConst(const ScriptVar_t& s) {
    std::cout << "value < SVT_BASEOBJT\n";
    return (T) s;
}

template<typename T>
typename std::enable_if<!(GetType<T>::value < SVT_BASEOBJ)
&& std::is_base_of<CustomVar,T>::value,T>::type
CastScriptVarConst(const ScriptVar_t& s) {
    std::cout << "CustomVar\n";
    return *s.as<T>();
}

template <typename T>
T ScriptVar_t::castConst() const {
    return CastScriptVarConst<T>(*this);
}

int main() {
    ScriptVar_t v;
    v.castConst<int>();
    v.castConst<CustomVar>();
}

答案 1 :(得分:1)

我不确定我是否理解正确,但这个元程序可以满足您的需求:

// Just a placeholder type for default template arguments
struct NoType { };

// Template to check whether a type is NoType. You can replace this with boost
// is_same<T, NoType> if you like.
template <typename T>
struct IsNoType  {
    static const bool value = false;
};

template <>
struct IsNoType<NoType>  {
    static const bool value = true;
};


// Template for matching the expressions and their corresponding types
// This could be done more nicely using variadic templates but no MSVC
// version supports them currently.
// You can specify up to 8 conditions and types. If you specify more,
// the code will break :) You can add more easily by just expanding the
// number of lines and parameters though.
template <
    bool p0 = false, typename t0 = NoType,
    bool p1 = false, typename t1 = NoType,
    bool p2 = false, typename t2 = NoType,
    bool p3 = false, typename t3 = NoType,
    bool p4 = false, typename t4 = NoType,
    bool p5 = false, typename t5 = NoType,
    bool p6 = false, typename t6 = NoType,
    bool p7 = false, typename t7 = NoType,

    // This must not be changed/overriden/specified, it is used as a condition to stop the compiler loop, see below
    bool stop = true, typename stopT = NoType  
>
struct GetFirstMatchingType  {

};

// Specialization when the first element in the expression list is true.
// When this happens, we just return the first type as the ::type typedef.
template <
    typename t0,
    bool p1, typename t1,
    bool p2, typename t2,
    bool p3, typename t3,
    bool p4, typename t4,
    bool p5, typename t5,
    bool p6, typename t6,
    bool p7, typename t7,
    bool p8, typename t8
>
struct GetFirstMatchingType<true, t0, p1, t1, p2, t2, p3, t3, p4, t4, p5, t5, p6, t6, p7, t7, p8, t8>  {
    typedef t0 type;

    // Check that the type is not NoType. If it is, it means all arguments are false and we should fail.
    // In case of a non-C++11 compiler, you can throw any other compiler error here or use BOOST_STATIC_ASSERT
    static_assert(!IsNoType<t0>::value, "No expression has been matched, don't know what type to return!");
};

// Specialization when the first type is false. If this happens, we cyclically rotate
// the sequence so that p0, t0 becomes p8, t8. The compiler keeps expanding this
// until it finds true as the first element. Note that this will always happen because
// the stop argument in the base template is set to true.
template <
    typename t0,
    bool p1, typename t1,
    bool p2, typename t2,
    bool p3, typename t3,
    bool p4, typename t4,
    bool p5, typename t5,
    bool p6, typename t6,
    bool p7, typename t7,
    bool p8, typename t8
>
struct GetFirstMatchingType<false, t0, p1, t1, p2, t2, p3, t3, p4, t4, p5, t5, p6, t6, p7, t7, p8, t8>  {
    typedef typename GetFirstMatchingType<p1, t1, p2, t2, p3, t3, p4, t4, p5, t5, p6, t6, p7, t7, p8, t8, false, t0>::type type;
};

int main()  {

// Evaluates to int myVar1 if int is 4 bytes, or __int32 myVar1 if __int32 is 4 bytes and int is not 4 bytes
GetFirstMatchingType<
    sizeof(int) == 4, int,
    sizeof(__int32) == 4, __int32
>::type myVar1;

// Evaluates to short myVar on my platform
GetFirstMatchingType<
    sizeof(int) == 5, int,
    sizeof(short) == 2, short
>::type myVar2;

// Also evaluates to short myVar on my platform
GetFirstMatchingType<
    sizeof(int) == 5, int,
    sizeof(short) == 2, short,
    sizeof(int) == 4, int
>::type myVar3;

// Throws an error (error C2338: No expression has been matched, don't know what type to return!)
GetFirstMatchingType<
    sizeof(int) == 5, int,
    sizeof(long) == 5, long,
    sizeof(short) == 3, short
>::type myVar4;
}

在MSVC 2010上测试过,但它应该适用于任何符合C ++标准的编译器,如GCC或Clang。

EDIT
以下是使用上述代码解决问题的示例:

struct ScriptVar_t;

struct CastScriptVar1 {
    template<typename T> static T castConst(const ScriptVar_t& s) { return (T) s; }
};

struct CastScriptVar2 {
    template<typename T> static T castConst(const ScriptVar_t& s) { return *s.as<T>(); }
};

struct ScriptVar_t {
    operator int() const { return 0; }
    operator float() const { return 0.0f; }
    operator BaseObject() const { return BaseObject(); }
    template<typename T> T* as() const { return NULL; }

    template <typename T> T castConst() const { 
        return GetFirstMatchingType<
            !boost::is_base_of<CustomVar, T>::value, CastScriptVar1,
            GetType<T>::value >= SVT_BASEOBJ, CastScriptVar2
            // Add more conditions & casts here
        >::type::castConst<T>(*this);
    }
};

int main()
{
    ScriptVar_t v;
    v.castConst<int>();
    v.castConst<CustomVar>();

    return 0;
}

可以使用boost :: tuple和boost :: mpl重写它来摆脱奇怪的可变参数模板。

编辑2:看来我之前的编辑消失了,我把它放回去了

答案 2 :(得分:1)

如果我理解正确,我会使用查找表 cast-functors和一个元函数来计算偏移量 表

另一种方法是使用基于类型的查找表 标签和仿函数。然后pick_cast会选择正确的标记 而不是int。如果做出决定,这可能更容易阅读 表变得很大。

#include <boost/type_traits.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/if.hpp>
#include <boost/mpl/at.hpp>

struct BaseObject {};
struct CustomVar {};

namespace mpl = boost::mpl;

struct ScriptVar_t {
    operator int() const { return 0; }
    operator float() const { return 0.0f; }
    operator BaseObject() const { return BaseObject(); }
    template<typename T> T* as() const { return NULL; }
    template <typename T> T castConst() const;
};

struct default_cast {
  template<typename T>
  T operator()(const ScriptVar_t& s) const { return (T) s; }
};

struct base_cast {
  template<typename T>
  T operator()(const ScriptVar_t& s) const { return *s.as<T>(); }
};

typedef mpl::vector< default_cast, base_cast > casts;

enum {
  DEFAULT = 0,
  BASE,
  END_OF_ENUM
};

// pick the right cast for T
template<typename T>
struct pick_cast {
  typedef typename mpl::if_< typename boost::is_base_of<CustomVar,T>::type,
                             mpl::int_<BASE>, mpl::int_<DEFAULT> >::type type;
};

template <typename T> T ScriptVar_t::castConst() const { 
  typedef typename mpl::at<casts, typename pick_cast<T>::type>::type func;
  return func().template operator()<T>(*this);
}

int main() {
    ScriptVar_t v;
    v.castConst<int>();
    v.castConst<CustomVar>();
}