开发者

Alexandrescus smart assert with return value

开发者 https://www.devze.com 2023-02-20 22:45 出处:网络
I\'m using Andrei Alexandrescus smart assert technique fo开发者_运维技巧r my assert facility, basically the interface is:

I'm using Andrei Alexandrescus smart assert technique fo开发者_运维技巧r my assert facility, basically the interface is:

ASSERT( foo == bar, "Foo doesn't equal bar!" )( foo )( bar );

As it turns out, often when the assert doesn't hold one wants to do an early exit, which leads me to the following overload:

ASSERT( foo == bar, "Foo doesn't equal bar!", 0 )( foo )( bar )

Sadly one limitation of the recursive macro trick which this is based upon is that the macro must end with another macro which kicks everything into gears, which leads me to the following piece of code for supporting a return value:

#define ASSERT3( expr, msg, ret ) \  
    if( (expr) ); \  
    else \  
    return ret + MakeAssert().AddContext( #expr, msg, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__ ).SMART_ASSERT_A 

By overloading operator+() for the Assert class this works out nicely, but here's the kicker and problem I'm currently facing: This method can't be used for either functions which returns void, constructors or destructors.


Though not quite satisfactorily, I post this just in case this can be a hint for you:

#define ASSERT3( expr, msg, ret, ... )                                  \
    if( (expr) );                                                       \
    else {                                                              \
        MakeAssert().AddContext(....).SMART_ASSERT_A __VA_ARGS__;       \
        return ret;                                                     \
    }

ASSERT3( foo == bar, "...", 0, ( foo )( bar ) ) // usage
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号