Compile-time assertion?

后端 未结 12 1022
春和景丽
春和景丽 2020-11-27 06:09

Is there a way I can assert that two constant expressions are equal at compile time?

e.g. I want this to cause a compile-time error

enum { foo=263,          


        
12条回答
  •  心在旅途
    2020-11-27 06:13

    There is also the trick to use a switch (..) statement. Kind of old style though. The case entry foo == bar has to be compile time evaluated and if it happens to be false the switch statement will cause an error. The compiler will also reduce it to "nothing".

    { 
      bool x=false; 
      switch (x) {
      case foo == bar:
        break;
      case false:
        // Compile time test that foo == bar
        break;
    }
    

提交回复
热议问题