บิตรายวัน (e) ของ C++ #38, C++23 ถ้าตัวแปร: ถ้า consteval {}

C++23 เพิ่มคำสั่ง if consteval {} พร้อมความสามารถในการทดสอบว่าโค้ดได้รับการประเมินอย่างต่อเนื่องหรือไม่

สิ่งนี้มีประโยชน์ในการแยกแยะระหว่างการใช้งานทางเลือก หรือเพื่อให้เกิดผลข้างเคียงของรันไทม์ เช่น การตรวจสอบ

constexpr int add(int a, int b) {
    if consteval {
        // consteval branch that can only use 
        // constant expression compatible code
        return a + b;
    } else {
        // not consteval, we can use fancy things 
        // like inline assembler, or intristics
        int ret;
        asm("addl %%ebx, %%eax;":"=a"(ret) : "a"(a), "b"(b));
        return ret;
    }
}

constexpr int side(int arg) {
    // Useful for side-effects such as logging...
    if !consteval {
        std::cout << "Not a constant expression, we can safely log.\n";
    }
    return arg*arg;
}

เปิดตัวอย่างใน Compiler Explorer