3

Starting in C++14, the assert macro can be used in functions even when they are defined as constexpr. I know this has to do with the fact that it evaluates to "true", but I'm having trouble figuring out what the actual code looks like.

Specifically, how do you build a macro that prints something when run in a constexpr function that is being evaluated at run time, but shuts this non-constexpr behavior off when in a constexpr function that is being evaluated at compile time.

cpplearner
  • 13,776
  • 2
  • 47
  • 72
Charles Ofria
  • 1,936
  • 12
  • 24
  • 1
    Interesting question ;) I expanded the assert macro and can boiled it down to a code snipped which shows that it works, but not why :-) So I asked a new question for that topic, because it is more general and has nothing to do with macros anymore. Maybe that helps on the way down to the underlying solution. If you like take a look here: https://stackoverflow.com/questions/47726503/calling-non-constexpr-functopn-from-constexpr-allowed-in-some-conditions – Klaus Dec 09 '17 at 07:51
  • Ahh -- I see. I had missed the fact that assert was no longer a macro, but now everything I've read makes a lot more sense and I'm pretty sure I know what I need. Thanks!! – Charles Ofria Dec 10 '17 at 01:27
  • But in the question I wrote nobody answers. And The fact, that it only compiles if the var is in range, makes the thing not clear to me, because the assert works and my code not. But the expansion of the macro works and it is still not understood :-) – Klaus Dec 10 '17 at 08:30

0 Answers0