如何在常量计算表达式中获得编译时错误?

Oli*_*liv 4 c++ gcc assertion constant-expression c++20

我有一个Assert用于评估断言的函数:

  • 如果前提条件在运行时失败,该函数将输出一条错误消息并终止程序。

  • 如果常量表达式中的前提条件失败,则会导致编译时错误。

我希望当断言在常量计算表达式中失败时,该函数也会生成编译时错误:

const int a = (Assert(false),0); //generate a runtime error 
                                 //=> I would like it generates a compile time error
Run Code Online (Sandbox Code Playgroud)

我考虑过使用std::is_constant_evaluatedcompiler-explorer

#include <type_traits>

using namespace std;

void runtime_error();

constexpr void compile_time_error(){} //should generates a compile time error

constexpr void Assert(bool value){
   if (value) return;
   if (is_constant_evaluated())
     compile_time_error();
   else
     runtime_error();
   }

void func(){
    const int a = (Assert(false),0);
    }
Run Code Online (Sandbox Code Playgroud)

我只使用 GCC,我寻找了一个会导致编译时错误的内置函数,该函数是 constexpr,但没有找到。

是否有任何技巧可以在可以常量求值的表达式中获得编译时错误?

Kam*_*Cuk 6

您可以调用未定义的函数,从而导致编译时错误。或者,由于您无论如何都使用 gcc,因此您可以从常量部分内部调用属性错误函数,以在编译该单元期间导致编译时错误。为了使其工作,您必须在启用优化的情况下进行编译。

我发现 withstd::is_constant_expression它在 gcc 9.2 中不起作用,但我设法让它与__builtin_constant_p.

#include <type_traits>

constexpr void Assert(bool value) {
   if (__builtin_constant_p(value)) {
        if (!value) {
            extern __attribute__(( __error__ ( "error" ) ))
            void compile_time_error(void);
            compile_time_error();
        }
    } else {
        if (!value) {
            void runtime_error();
            runtime_error();
        }
   }
}

void func(int b) {
    const int a = (Assert(false), 0);
    Assert(b == 0);
}
Run Code Online (Sandbox Code Playgroud)

我曾经在 CI 中写过一个名为“curb”的库,它可以做这样的事情。