`std::alignment_of` versus `alignof`
Asked Answered
F

3

21

I've just stumbled upon the std::alignment_of type trait, and its soon-to-be friend std::alignment_of_v. They seem to have been specifically designed to be equivalent to a plain call to alignof, and the future addition of the _v helper indicates that it's not just a legacy bit.

What is the use of std::alignment_of (_v), when we have alignof ?

Fulmination answered 2/5, 2016 at 12:2 Comment(1)
To have true error when use with expression instead of use gnu-alignof-expression :-).Botulism
A
22

They are almost completely redundant. As @Revolver noted, they were introduced in different papers, and alignment_of comes from boost nearly verbatim.

But that does not mean the trait is useless.

A template<class...>class can be passed to other templates and used with metaprogramming. Operators like alignof cannot: you would have to write the template<class>class alignment_of before you could pass it to metaprogramming facilities.

Now the same could be said of sizeof needing a std::size_of<class> template.

...

The addition of _v was because they swept every ::value integral_constant-type template in std and added a _v variable template. Considering which ones where worthy and which not would be bikeshed painting and nearly pointless: it is easier to do every one than spend effort picking the worthy ones to do. It being done is not evidence the feature is not obsolete.

Andro answered 2/5, 2016 at 13:38 Comment(1)
The usefulness of the metafunction itself hadn't crossed my mind. That is a very good point, thank you !Fulmination
R
5

The std::alignment_of is introduced as port of Boost type traits library. It predates C++11 and alignof keyword. That trait was superseded by alignof operator, but it was kept mainly for compatibility purposes (so you could just replace boost:: with std::) and _v variable alias is introduced for consistency with other parts of the library.

Rhombohedral answered 2/5, 2016 at 12:12 Comment(3)
From your answer it seems as if they were introduced separately, when they were not (both since C++11).Bennybenoit
@black technically they were introduced separately (in different papers). The point of my answer was that it was a part of pre-C++11 library before inclusion in standard.Rhombohedral
alignment_of was in TR1.Busywork
S
1

One nice thing I found in practice is that alignof is simply syntactically invalid for old code, whereas alignment_of is just a standard template. So you can stay compatible with older compilers by providing an implementation of alignment_of using whatever alternative syntax your compiler accepts, then use that everywhere.

Shammy answered 5/3, 2021 at 23:53 Comment(4)
Can you give an example where alignof would've been syntactically incorrect but alignment_of would've worked?Revisionism
@AyxanHaqverdili: Anywhere in old (pre-C++11) code.Shammy
Oh, if you're bringing in your own implementation that just masks compiler-specific code in pre-C++11 code, you could also implement alignof as a macro.Revisionism
@AyxanHaqverdili: Sure, you can implement lots of things as a macro.Shammy

© 2022 - 2024 — McMap. All rights reserved.