Brandon
Brandon

Reputation: 23500

constexpr variable must be initialized by a const expression

With the code below, I get Constexpr variable 'max_digits' must be initialized by a constant expression with Apple clang version 12.0.5 (clang-1205.0.22.11) running C++14.

However, if I #define WORKING_CODE it works fine with base_two_digits being a global function. If I move the contents of base_two_digits into log_base_10, I'll get an error (this is when #undef WORKING_CODE is present).

Why is it not constexpr, if WORKING_CODE is undefined?

#include <limits>
#include <cstdint>
#include <cmath>

template<typename T>
constexpr T power_of_10(T n)
{
    return n < 0 ? 0 : n == 0 ? 1 : (n == 1 ? 10 : 10 * power_of_10(n - 1));
}

#ifdef WORKING_CODE
constexpr std::uint64_t base_two_digits(std::uint64_t value) {
    return value ? 8 * sizeof(value) - __builtin_clzll(value) : 0;
};
#endif

template<typename T>
constexpr T log_base_10(T value) {
    constexpr const unsigned guess[65] = {
        0 ,0 ,0 ,0 , 1 ,1 ,1 , 2 ,2 ,2 ,
        3 ,3 ,3 ,3 , 4 ,4 ,4 , 5 ,5 ,5 ,
        6 ,6 ,6 ,6 , 7 ,7 ,7 , 8 ,8 ,8 ,
        9 ,9 ,9 ,9 , 10,10,10, 11,11,11,
        12,12,12,12, 13,13,13, 14,14,14,
        15,15,15,15, 16,16,16, 17,17,17,
        18,18,18,18, 19
    };
    
    #ifdef WORKING_CODE
    T digits = guess[base_two_digits(value)];
    #else
    constexpr auto index = value ? 8 * sizeof(value) - __builtin_clzll(value) : 0;
    T digits = guess[index];
    #endif
    return digits + (value >= power_of_10(digits));
}

class BigInt {
private:
    static constexpr const std::uint32_t base = 1000;
    static constexpr const std::uint32_t max_digits = log_base_10(base) - 1; // 3;

public:
    BigInt() {}
};

int main() {
    BigInt p;

    return 0;
}

Upvotes: 3

Views: 3246

Answers (1)

Quimby
Quimby

Reputation: 19123

Let me simplify the code first (please ignore the nonsensical behaviour) to the following example illustrating the same issue:

template<typename T>
constexpr T log_base_10(T value) {
    constexpr auto index = value? 1:0;
    return 0;
}

int main() {
    constexpr auto foo = log_base_10(42);
    return 0;
}

The problem here is defining a constexpr variable index using the function parameter which is not necessarily constexpr itself. If the value would have been supplied at run-time, the initialization of index would be impossible.

The issue does not arise in WORKING_CODE because the temporary index in

T digits = guess[base_two_digits(value)];

is not forced to be constexpr (but could be in this case). On the other hand,

constexpr auto index = value ? 8 * sizeof(value) - __builtin_clzll(value) : 0;

is forced.

I am not 100% sure whether the compiler could give you a pass here (w.r.t The Standard) since the function template is only instantiated for a compile-time call. But because the instantiated function cannot be compiled and called at run-time, the errors should be justified.

The solution is to simply use ordinary non-constexpr index.

Upvotes: 5

Related Questions