为什么 constexpr 无法绕过 constexpr 求值?

Why if constexpr fails to bypass constexpr evaluation?

我正在为使用宏的类型分派构建静态循环。这是 到目前为止我取得了什么。

#define LOOP(n, f)                                            \
    static_assert(n <= 8 && "static loop size should <= 8");  \
    do {                                                      \
        if constexpr (n >= 8)                                 \
            f(std::integral_constant<size_t, n - 8>());       \
        if constexpr (n >= 7)                                 \
            f(std::integral_constant<size_t, n - 7>());       \
        if constexpr (n >= 6)                                 \
            f(std::integral_constant<size_t, n - 6>());       \
        if constexpr (n >= 5)                                 \
            f(std::integral_constant<size_t, n - 5>());       \
        if constexpr (n >= 4)                                 \
            f(std::integral_constant<size_t, n - 4>());       \
        if constexpr (n >= 3)                                 \
            f(std::integral_constant<size_t, n - 3>());       \
        if constexpr (n >= 2)                                 \
            f(std::integral_constant<size_t, n - 2>());       \
        if constexpr (n >= 1)                                 \
            f(std::integral_constant<size_t, n - 1>());       \
    } while (0);

template <typename T> constexpr size_t tupleSize(T&) { return tuple_size_v<T>; }

int main() {
    auto t = std::make_tuple(1, "string", 0.2, 3, 1, 1, 1);
    LOOP(tupleSize(t), [&](auto i) { cout << std::get<i>(t) << endl; });
    return 0;
}

还有神箭linkhttps://godbolt.org/z/GcMZI3

问题是,为什么前四个分支编译失败?

不要使用宏,而是使用函数模板。 if constexpr 的工作原理是 丢弃 未采用的分支,具体取决于模板的当前 实例化

template <std::size_t n, typename F>
void loop(F&& f)
{
    static_assert(n <= 8 && "static loop size should <= 8");
    if constexpr (n >= 8)
        f(std::integral_constant<size_t, n - 8>());
    if constexpr (n >= 7)
        f(std::integral_constant<size_t, n - 7>());
    if constexpr (n >= 6)
        f(std::integral_constant<size_t, n - 6>());
    if constexpr (n >= 5)
        f(std::integral_constant<size_t, n - 5>());
    if constexpr (n >= 4)
        f(std::integral_constant<size_t, n - 4>());
    if constexpr (n >= 3)
        f(std::integral_constant<size_t, n - 3>());
    if constexpr (n >= 2)
        f(std::integral_constant<size_t, n - 2>());
    if constexpr (n >= 1)
        f(std::integral_constant<size_t, n - 1>());
}

用法:

int main() {
    constexpr auto t = std::make_tuple(1, "string", 0.2, 3);
    loop<tupleSize(t)>([&](auto i) { cout << std::get<i>(t) << endl; });
    return 0;
}

live example on godbolt.org


来自cppreference

If a constexpr if statement appears inside a templated entity, and if condition is not value-dependent after instantiation, the discarded statement is not instantiated when the enclosing template is instantiated.

Outside a template, a discarded statement is fully checked. if constexpr is not a substitute for the #if preprocessing directive