I am currently using the following solution:
#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));
};