静态断言(编译时断言):static_assert

《c++程序设计语言(第四版)》2.4.3章节

static_assert(A,S) A为常量表达式,当A不为true时,编译不能通过,S会被当做一条编译器错误信息输出。

#define debug qDebug()<<

constexpr int C = 299792458;

void debugValue(int value)
{
    const int maxSpeed = 10000000000;
    static_assert(maxSpeed < C,"You can't go faster than the speed of light");
    debug "value = "<<value;
}

int main(int argc, char *argv[])
{
    debugValue(2);
}

猜你喜欢

转载自blog.csdn.net/kenfan1647/article/details/113788587