我曾期望编译器允许在涉及 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";
}
任何解释/解决方法都会有所帮助