Branch with if constexpr() on constinit variables?

44 Views Asked by At

In the following code I try to constexpr construct a data structure and then use one of the constexpr initialized members (a_) in a method to branch with if constexpr().

From a logical standpoint AFAIK nothing speaks against it: Of course, the memory location is only known at runtime, the this-pointer passed to the method therefore will only be known at runtime. However, constexpr branch elision can be done before, when the constexpr data is written into the binary, the compiler could take another turn and constexpr elide one of the branches because a_ is known for every struct. Of course this would mean that the compiler generates instructions for multiple methods, one for each branch.

Am I making a mistaken or are compilers lacking behind in this departement?

(Erroneous) Demo

#include <array>
#include <initializer_list>
#include <cstdio>
#include <algorithm>

struct channel
{
    constexpr channel(int a)
        :   a_{a}
    {}
    
    constexpr void compile_time_decide() const {
        if constexpr (a_ > 5) {
            printf("Decided at compile time!\n");
        } else {
            printf("Decided at compile time!\n");
        }
    }

    const int a_;
};

template <size_t N>
struct light_service
{
    constexpr light_service(std::array<channel, N> channels)
        :   channels_{channels}
    { }

    void runtime_invoke() {
        std::for_each(channels_.begin(), channels_.end(), [](channel& ch){ ch.compile_time_decide(); });
    }

    std::array<channel, N> channels_;
};

constinit light_service<10> light{{1,2,3,4,5,6,7,8,9,10}};

int main()
{
    light.runtime_invoke();
}
0

There are 0 best solutions below