Why is it not constexpr?

#include <iostream> union gc_bits { size_t value; struct { size_t arena : 2; } bits; constexpr gc_bits(size_t value_) : value(value_) { } }; static constexpr size_t get_max_arenas() { return gc_bits(~0ULL).bits.arena; } size_t current_colour[get_max_arenas()]; // error int main() { std::cout << get_max_arenas() << std::endl; } 

An array declaration is erroneous because get_max_arenas is not constexpr. I do not understand why this should be so.

+6
source share
1 answer

Rephrase your program a bit:

 static constexpr auto gma = get_max_arenas(); size_t current_colour[gma]; // error 

gives a Clang error:

reading bits of a member of 'union with active value' 'is not allowed in constant expression

The reason you get this error is because the constructor sets value , and then you try to read bits . This is forbidden, as @gurka commented.

Standard Quote:

[expr.const]

2 The conditional expression e is an expression of the kernel constant if the estimate e, following the rules of the abstract machine (1.9), evaluates one of the following expressions:

(2.8) - lvalue-to-r transformation (4.1) or modification (5.18, 5.2.6, 5.3.2), which is applied to a glvalue that refers to an inactive member of a union or its subobject;

+7
source

All Articles