Re: [eigen] Alignment of a derived class

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


Am 20.07.2012 um 12:08 schrieb Gael Guennebaud:

> Hi,
> 
>> I have another question:
>> 
>> I saw that you define
>>   typedef void eigen_aligned_operator_new_marker_type;
>> in the macro
>>   EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF
>> in Memory.h
>> 
>> Do you have a structure in Eigen that checks if the typedef is defined, similar to the second example here:
>> http://en.wikipedia.org/wiki/SFINAE
> 
> no, and actually I'm not sure eigen_aligned_operator_new_marker_type
> is still needed.
> 
>> If Eigen provides this I would not have to define it myself.
>> 
>> Basically what I want to do is to use boost::conditional to check if I have to use the aligned allocator for a vector.
> 
> Boost offers what you need to test if
> eigen_aligned_operator_new_marker_type is present. I don't remember in
> which package though.
> 
> gael
> 
>> 
>> thank you,
>> Martin


Hi,

Unfortunately I haven't found anything suitable in boost. I checked boost::enable_if but it seems it is meant to be used in a different way (please correct me if I am wrong). Maybe someone else on the mailing-list knows the package that can do test.

I attached a small example. ContainerWithCheck is doing the boost::conditional check. ContainerWithoutCheck simply uses the Eigen::aligned_allocator for every input data.

The question is: Is ContainerWithoutCheck <UnAligned> well defined? Because then I could skip all the SFINAE stuff.

best regards
Martin

#include <boost/type_traits/conditional.hpp>
#include <Eigen/Core>
#include <Eigen/StdVector>

template <class T>
class HasEigenAlignedOperatorNew
{
  private:

    typedef char Yes [1];
    typedef char No  [2];

    template <class U> static Yes&
    test (typename U::eigen_aligned_operator_new_marker_type*);

    template <class> static No&
    test (...);

  public:

    static const bool value = sizeof (test <T> (0)) == sizeof (Yes);
};

struct Aligned
{
  Eigen::Vector4f vec4f;
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
};

struct UnAligned
{
  int i;
};

template <class T>
struct ContainerWithCheck
{
  typedef typename boost::conditional <HasEigenAlignedOperatorNew <T>::value, std::vector <T, Eigen::aligned_allocator <T> >, std::vector <T> >::type Vector;

  Vector storage;
};

template <class T>
struct ContainerWithoutCheck
{
  typedef std::vector <T, Eigen::aligned_allocator <T> > Vector;

  Vector storage;
};

int main ()
{
  ContainerWithCheck <Aligned>   c_with_aligned;
  ContainerWithCheck <UnAligned> c_with_unaligned;

  ContainerWithoutCheck <Aligned>   c_without_aligned;
  ContainerWithoutCheck <UnAligned> c_without_unaligned; // Any problems here?

  return (0);
}





Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/