Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this not a 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;
}

The array declaration errors out because get_max_arenas is not a constexpr. I'm not clear on why this should be so.

like image 507
DrPizza Avatar asked Sep 17 '15 06:09

DrPizza


People also ask

How do I know if a function is constexpr?

The easiest way to check whether a function (e.g., foo ) is constexpr is to assign its return value to a constexpr as below: constexpr auto i = foo(); if the returned value is not constexpr compilation will fail.

Why is Reinterpret_cast not constexpr?

There are very few valid uses of reinterpret_cast , most of them result in UB. Plus it is practically impossible to check if the use is valid. So reinterpret_cast is not allowed during compilation, i.e. it is not allowed in constexpr.

Is not usable as a constexpr function?

It means that compiler makes most general assumptions when compiling this function. And this assumption is that arguments are non-compile-time. It means that compiler can't guarantee that your if-constexpr is always compile-time, hence the error about it.

When can a function be constexpr?

A constexpr function is one whose return value is computable at compile time when consuming code requires it. Consuming code requires the return value at compile time to initialize a constexpr variable, or to provide a non-type template argument.


1 Answers

Slightly rephrasing your program:

static constexpr auto gma = get_max_arenas();

size_t current_colour[gma]; // error

gives the Clang error:

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

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

Standard quote:

[expr.const]

2 A conditional-expression e is a core constant expression unless the evaluation of e, following the rules of the abstract machine (1.9), would evaluate one of the following expressions:

(2.8) — an lvalue-to-rvalue conversion (4.1) or modification (5.18, 5.2.6, 5.3.2) that is applied to a glvalue that refers to a non-active member of a union or a subobject thereof;

like image 93
TemplateRex Avatar answered Oct 02 '22 05:10

TemplateRex