writes:
> Hi!
>
> I am for defensive programming and am working on a code stuffed with
> quite a lot of assert-like statements, like
>
> Asserts(pos[i]==pos[i],"Position #"+to_string(i)+" is NaN");
>
> where Asserts is
>
> #define Asserts(expr,s) \
> if(!(expr))
> Framework::Asserts_Fail(__STRING(expr),__FILE__,__ LINE__,__PRETTY_FUNCTION__,s)
>
> and Asserts_Fail() displays a bunch of info and terminates the
> program.
>
> My question: a typical assert falls through 99.999% of the time as
> if(!(expr)) is false. Am I guaranteed though, that the std::string
> "Position #number is NaN" would not be constructed in vain _every
> time_ this assert is checked? My understanding is that since this is a
> macro, not a function call, then it is only constructed when the
> assert actually clicks and Asserts_Fail() is called. If Asserts() was
> made to be a function call instead of a macro, then I might not be as
> lucky.
>
> Can anyone confirm or contradict this?
Yes, emacs can.
Put your source in some buffer named test.c:
------------------------------------------------------------------------
#define Asserts(expr,s) \
if(!(expr)) Framework::Asserts_Fail(__STRING(expr),__FILE__,__ LINE__,__PRETTY_FUNCTION__,s)
Asserts(pos[i]==pos[i],"Position #"+to_string(i)+" is NaN");
------------------------------------------------------------------------
Select it and type C-x C-e ; it will open another buffer containing
the code with the macros expanded:
------------------------------------------------------------------------
if(!(pos[i]==pos[i])) Framework::Asserts_Fail(__STRING(pos[i]==pos[i]),"a.c",5,__PRETTY_FUNCTION__,"Position #"+to_string(i)+" is NaN");
------------------------------------------------------------------------
If you don't have emacs, you can run the c preprocessor on the source:
gcc -E -o test.e test.c && cat test.e
So you can see that indeed, the expansion of the macro is purely
textual, and the the concatenation will occur only when the assertion
is false.
--
__Pascal Bourguignon__