13

I would like to do this for usages which may be inefficient but not necessarily incorrect.

Clinton
  • 22,361
  • 15
  • 67
  • 163

3 Answers3

6

No.

An assertion failure indicates a problem preventing the program from being completed (be that execution [run-time assertions], or compilation [static assertions]).

In truth, an implementation is allowed to do anything as long as they emit a diagnostic (including continuing execution). But, in practice, mainstream toolchains will all behave pretty much the same: they will error out. You certainly can't hack them to something user-defined.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • compile-time assertion failure prevents the code from *compiling*. running is out of question. – Nawaz May 24 '11 at 09:20
  • @Nawaz: I was using a more general meaning of "to run". Poor choice of word, perhaps. Allow me to pick a different one. – Lightness Races in Orbit May 24 '11 at 09:21
  • 5
    @Tomalak, @Nawaz - Although, if I may indulge in a bit of a language lawyering, the Standard never requires the compilation to halt if the compiler encounters ill-formed code (including a failed `static_assert`) -- only that a diagnostic message must be emitted. After this, the compiler is free to do whatever it wants, including finishing the compilation anyway. – JohannesD May 24 '11 at 09:51
  • 1
    @Tomalak : @JohannesD is correct. Check out [this](http://stackoverflow.com/questions/5177167/why-does-my-c-compiler-allow-recursive-calls-to-main/5177289#5177289) answer. A compiler is free to do whatever it wants to do. – Prasoon Saurav May 24 '11 at 11:48
  • @Prasoon: Oh, believe me, it's not that I don't trust him. – Lightness Races in Orbit May 24 '11 at 12:05
3

The attributes are introduced in C++0x for that purpose. See http://docwiki.embarcadero.com/RADStudio/en/C%2B%2B0x_attribute_deprecated for an example.

vines
  • 5,160
  • 1
  • 27
  • 49
0

Not as Standard, no. You can find #warning in many compilers, but that's really not the same in most situations.

一二三
  • 21,059
  • 11
  • 65
  • 74
Puppy
  • 144,682
  • 38
  • 256
  • 465