It is entirely possible that Eigen is just a terribly written library (or just poorly-thought out); just because something is online doesn't make it true. For example:
Passing objects by value is almost always a very bad idea in C++, as this means useless copies, and one should pass them by reference instead.
This is not good advice in general, depending on the object. It is sometimes necessary pre-C++11 (because you might want an object to be uncopyable), but in C++11, it is never necessary. You might still do it, but it is never necessary to always pass a value by reference. You can just move it by value if it contains allocated memory or something. Obviously, if it's a "look-but-don't-touch" sort of thing, const&
is fine.
Simple struct objects, presumably like Eigen's Vector2d
are probably cheap enough to copy (especially in x86-64, where pointers are 64-bits) that the copy won't mean much in terms of performance. At the same time, it is overhead (theoretically), so if you're in performance critical code, it may help.
Then again, it may not.
The particular crash issue that Eigen seems to be talking about has to do with alignment of objects. However, most C++03 compiler-specific alignment support guarantees that alignment in all cases. So there's no reason that should "make your program crash!". I've never seen an SSE/AltaVec/etc-based library that used compiler-specific alignment declarations that caused crashes with value parameters. And I've used quite a few.
So if they're having some kind of crash problem with this, then I would consider Eigen to be of... dubious merit. Not without further investigation.
Also, if an object is unsafe to pass by value, as the Eigen docs suggest, then the proper way to handle this would be to make the object non-copy-constructable. Copy assignment would be fine, since it requires an already existing object. However, Eigen doesn't do this, which again suggests that the developers missed some of the finer points of API design.
However, for the record, C++11 has the alignas
keyword, which is a standard way to declare that an object shall be of a certain alignment.
Also, why is there no problem with returning such objects by value?
Who says that there isn't (noting the copying problem, not the alignment problem)? The difference is that you can't return a temporary value by reference. So they're not doing it because it's not possible.
std::aligned_storage<>
? – Appsaligned_storage<>
object (via aliasing) and pass thealigned_storage<>
object by value. The other side of the call would simply need to know how to alias the data. A sensible API would implement those trivially copyable types in terms ofaligned_storage<>
so the consumer would never need to know about it or do any aliasing manually. – Appsalignas
takes a number.alignas(16)
means align on 16-byte boundaries. If the compiler cannot do so, the program is ill-formed (diagnostic required).alignas(T)
is defined to bealignas(alignof(T))
. – Pahangalignas
took a number, and didn't realize there was both aalignas
andalignof
. My bad. Comments deleted. – Abranchiate__m128
is 16-bit aligned and cannot be passed by value. – Sheppalignas
allow you to pass the compiler intrinsic__m128
by value? – Jago