iampat
iampat

Reputation: 1101

Enforce Memory alignment in C++

I want to design an API, which internally uses EIGEN.

Based on http://eigen.tuxfamily.org/dox/TopicPassingByValue.html, if a class have a Eigen object as member, it can not be passed by value.

Is there any straight forward way to tell compiler (e.g. g++) the my object can not be passed by value?

Upvotes: 2

Views: 736

Answers (3)

Kerrek SB
Kerrek SB

Reputation: 477464

You can simply make the copy constructor unavailable. You can achieve this by using Boost and inheriting from boost::noncopyable, or by making the copy constructor private:

struct Foo
{
private:
    Foo(Foo const &) { }
};

Or in the new C++ by explicitly deleting it:

struct Foo
{
    Foo(Foo const &) = delete;
    Foo(Foo &&)      = delete;
};

You should probably also make your class unassignable by doing the same to the assignment operator (and boost::noncopyable takes care of this for you).

Upvotes: 6

Kaz
Kaz

Reputation: 58627

To prevent a C++ object from being copied, declare a copy constructor and assignment operator, but make those functions private. (Since they are not called by anything, you don't have to bother implementing them.)

The documentation you cited looks bogus. How is it that this Eigen::Vector2d object is able to achieve its proper alignment in the original object, and why wouldn't the copy object have the same alignment?

The extraordinary piece of information required for that to make sense is not given.

Upvotes: 2

user405725
user405725

Reputation:

Just make a copy constructor/copy operator private.

Upvotes: 0

Related Questions