On 23/10/2019 10:12, Öö Tiib wrote:
> On Wednesday, 23 October 2019 09:59:14 UTC+3, Frederick Gotham wrote:
>> So I was playing around with aligned_storage, and I had something like:
>>
>> aligned_storage< max(sizeof(A), sizeof(B)),
>> max(alignof(A), alignof(B)) >::type buf;
>>
>>
>> But then this wouldn't compile because "max" isn't a constexpr. So next I decided I'd just use a MAX macro instead.
>>
>> So then I was perusing a reference of the C++11 standard library and I came across aligned_union, which seemed to be exactly what I needed, as the previous code would become:
>>
>> aligned_union<1, A, B>::type buf;
>>
>> I'm curious though. . .
>>
>> How does aligned_union work fine in C++11 if max didn't become a constexpr until C++14 ?
>
> I am not 100% sure but I trust that max can't work because max is required to
> return reference to one of its arguments. C++11 allowed reference
> constant expressions only to designate objects of static storage duration
> or functions. The sizeof invocations are not producing objects of static
> storage duration. The later standards relaxed those requirements in
> major ways.
A simple "(a > b) ? a : b" will be a constant expression in C++, as long
as "a" and "b" are constant expressions, so I guess aligned_union uses
something like that directly rather than std::max.