https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85747

--- Comment #7 from Antony Polukhin <antoshkka at gmail dot com> ---
(In reply to Jakub Jelinek from comment #6)
> IMHO just use constexpr if you care about compile time evaluation
> guarantees, that is what it has been added for.

Fair point. Overcomplicated logic on the frontend does not seem right. But from
my (not experienced) point of view there are some low hanging fruits here.

I assume that frontend uses some kind of `__builtin_constant_p` to distinguish
between constexpr evaluation or not. Adjusting that function slightly could
produce better code out of the box on some optimization levels:

static int generate() {
    int a[7] = {3, 7, 4, 2, 8, 0, 1};
    static_assert(
        __builtin_constant_p(a + 0),
        "Immediate usage of variable initialized by constant should be a
constant expression"
    );
    sort(a + 0, a + 7); // __builtin_constant_p returns `true` => constexpr
call
    static_assert(
        __builtin_constant_p(a + 0),
        "Value after constexpr function call should be a constant"
    );
    return a[0] + a[6];
}

Reply via email to