【问题标题】:Conditional Compile-time Type mapping based on template parameter基于模板参数的条件编译时类型映射
【发布时间】:2014-06-17 13:10:45
【问题描述】:

我如何在编译时检查模板参数是否具有特定的枚举,如果是,则获取该枚举值。

我正在为模板参数T 寻找类似的东西,它可能(或可能没有)具有枚举类型NeedsToAlign

compile_time_if (T has NeedToAlign) {
 // only then compile the following
 EIGEN_MAKE_ALIGNED_OPERATOR_IF(typename T::NeedToAlign)
}

继续阅读以了解我的实际问题:

所以我有一个名为Payload 的类,它定义了一个枚举NeedsToAlign,如下所示:

struct Payload<int N> {
 typedef Eigen::Matrix<float, N, 1> DataType;
 DataType data;

  enum {
    NeedToAlign = (sizeof(DataType)%16)==0,  ///< Check for alignement need
  };
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(EigenNeedsToAlign)
};

虽然与我的实际问题无关,但对于某些固定大小的DataType,使用Eigen library to generates aligned pointers 需要EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF() 宏。

现在假设我有另一个模板类,它只存储 T 类型的变量,它可能是 Payload 类型或更简单的类型,例如 int/float 等。

template <typename T>
class A {
 T value;

// My goal:
// if T is of type Payload we need to enable the following:
// EIGEN_MAKE_ALIGNED_OPERATOR_IF(T::NeedToAlign) 

};

请注意,我不能简单地使用 EIGEN_MAKE_ALIGNED_OPERATOR_IF(typename T::NeedToAlign),因为我希望 class A 也可以使用 say T = int 进行编译。

是否有针对这些的 Boost MPL 解决方案?这是我到目前为止所能想到的:

typedef typename boost::mpl::if_< 
        has_EigenNeedsToAlign<Payload>,
        typename Payload::NeedToAlign,
        boost::mpl::int_< 0 >
                                >::type result;
 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(result::value);

但对于T = int 类型,上述方法当然会失败。

我可以看到解决方案的一种方法是对具有 T::NeedToAlign 的 Payload 类型或 T 类型的 A 类进行某种部分特化。但是有没有一种 Boost MPL 方法可以做到这一点?

您可能已经知道,我对此很陌生。


更新1:

我想出了一种方法,即通过如下方式派生 A 类:

template <typename T>
class A : public EigenAllocatorHelper<T>{
 T value;
 ....    
};

EigenAllocatorHelper 通过部分特化注入适当的行为(如下所示):

template<typename T, class Enable = void>
class EigenAllocatorHelper {
};

BOOST_MPL_HAS_XXX_TRAIT_DEF(NeedsToAlignMPL)

template< typename T >
class EigenAllocatorHelper< T, typename boost::enable_if< has_NeedsToAlignMPL<T> >::type > {
public:
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(typename T::NeedsToAlign);
};

我还必须使用typedef boost::mpl::int_&lt;(sizeof(DataType)%16)==0&gt; NeedToAlignMPL;,因为当我使用简单枚举时mpl has_xxx 不起作用。

这个解决方案对我来说看起来不错,事实上我也可以在其他地方重用这个 EigenAllocatorHelper 类。但如果有更好的 MPL 魔法不需要这个,请告诉我。

【问题讨论】:

  • 宏有什么作用?
  • 它基本上定义了几个函数,比如 new()。 See herehere 解释一下

标签: c++ templates boost eigen boost-mpl


【解决方案1】:

我目前正在使用以下解决方案:

#include <boost/utility/enable_if.hpp>
#include <boost/mpl/has_xxx.hpp>
#include <boost/mpl/and.hpp>

//! The macro is used for enable_if if T is an Eigen Type
#define ENABLE_IF_EIGEN_TYPE(T)\
    typename boost::enable_if< is_eigen_type<T> >::type

/** The macro enables Eigen new() when required. T can be any type
 *
 * Example Usage:
 *  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_REQUIRED(Eigen::Vector2d) will enable Eigen's new()
 *  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_REQUIRED(Eigen::Vector3d) will NOT
 *  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_REQUIRED(int) will NOT
 */
#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_REQUIRED(T)\
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(requires_eigen_new_allign<T>::value)

namespace detail {
BOOST_MPL_HAS_XXX_TRAIT_DEF(Scalar)
BOOST_MPL_HAS_XXX_TRAIT_DEF(Index)
BOOST_MPL_HAS_XXX_TRAIT_DEF(StorageKind)
}

/**
 * Traits for checking if T is indeed an Eigen Type
 * @tparam T any Type
 *
 * Example Usage:
 * is_eigen_type<int>::value // evaluates to false
 * is_eigen_type<int>::type // evaluates to false_type
 * is_eigen_type<Eigen::Vector2d>::value // evaluates to true
 * is_eigen_type<Eigen::Vector2d>::type // true_type
 */
template<typename T>
struct is_eigen_type:
    boost::mpl::and_<
      detail::has_Scalar<T>,
      detail::has_Index<T>,
      detail::has_StorageKind<T> > {
};


template<class T, class Enable = void>
struct requires_eigen_new_allign {
  static const bool value = false;
};

template<class T>
struct requires_eigen_new_allign<T, ENABLE_IF_EIGEN_TYPE(T)> {
  typedef typename T::Scalar Scalar;
  static const bool value = (((T::SizeAtCompileTime) != Eigen::Dynamic)
      && ((sizeof(Scalar) * (T::SizeAtCompileTime)) % 16 == 0));
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-05
    • 2017-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多