0

我曾期望编译器允许在涉及 constexpr 的不可访问代码中出现无效或错误语句,如果:

#include <type_traits>
#include <iostream>

struct ret_t;
struct only_declared_t;

auto test = [](auto a) {
    if constexpr(std::is_invocable_r_v<ret_t, decltype(a), int>) {
        return a(42);
    } else {
        return 42;
    }

    // I expect to never reach that statement, but the compiler complains
    static_assert(false, "This code should never be reached."); 
    return only_declared{};
};

int main() {
    std::cerr << "Result is " << test(10) << "\n";
}

任何解释/解决方法都会有所帮助

4

1 回答 1

-1

static_assert在编译时工作,因此无论代码可访问性如何,它都会生成编译时错误。您可以assert用于运行时检查。assert不应评价。

于 2020-09-15T09:59:33.217 回答