How do I in compile time check if a template parameter has a particular enum and if that is true get that enum value.
I am looking for some thing like this for a template parameter T which may ( or may not) have an enum type NeedsToAlign :
compile_time_if (T has NeedToAlign) {
// only then compile the following
EIGEN_MAKE_ALIGNED_OPERATOR_IF(typename T::NeedToAlign)
}
Read on to see my actual problem:
So I have class called Payload which defines a enum NeedsToAlign as shown below:
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)
};
Though not related to my actual question, EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF() macro is required for using Eigen library to generates aligned pointers for certain fixed size DataType.
Now say I have another template class which just stores a variable of type T which may be a Payload type or more simpler types such as int/float etc.
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)
};
Note I cannot simply use EIGEN_MAKE_ALIGNED_OPERATOR_IF(typename T::NeedToAlign) since I want class A to also compile with say T = int.
Is there a Boost MPL solution for these? This is what I have been able to come up with till now:
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);
But of course the above fails for T = int types.
One way I can see a solution is to do some sort of partial specialization of class A for Payload types or T types which has T::NeedToAlign. But is there a Boost MPL way of doing this?
As you probably have figured out, I am very new to this.
Update1:
I have figured out one way which is by deriving class A as following:
template <typename T>
class A : public EigenAllocatorHelper<T>{
T value;
....
};
where EigenAllocatorHelper injects the appropriate behavior by partial specialization (shown below):
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);
};
Also i had to use typedef boost::mpl::int_<(sizeof(DataType)%16)==0> NeedToAlignMPL; since mpl has_xxx did not work when I used the simple enum.
This solution looks okay to me and infact I can reuse this EigenAllocatorHelper class elsewhere also. But if there is better MPL magic which does not require this, let me know.