发布构建断言/验证 [英] release build assertions/verifications

查看:80
本文介绍了发布构建断言/验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在发布版本中进行验证。如果验证

失败,则应显示错误消息,并且程序应该中止
。我需要这个解决方案是可移植的。然后我想到了

断言宏 - 它提供了所需的功能,但只有在未定义NDEBUG的情况下才会使用
。所以,我可以写这样的smth:


#if定义NDEBUG

#undef NDEBUG

#include< cassert>

#define NDEBUG

#else

#include< cassert>

#endif


不幸的是,NDEBUG可以定义为有意义的东西,

这种情况​​下它的价值会丢失。


可以有没有人想到使用断言宏进行

验证的其他方法?

其他任何选项怎么样?


问候,

Angel Tsankov

I need to make verifications in release builds. If a verification
fails, an error message should be displayed and the program should be
aborted. I need this solution to be portable. Then I thought of the
assert macro - it provides the desired functionality but only if
NDEBUG is not defined. So, I could write smth like this:

#if defined NDEBUG
#undef NDEBUG
#include <cassert>
#define NDEBUG
#else
#include <cassert>
#endif

Unfortunately, NDEBUG could be defined to something meaningful, in
which case it''s value would be lost.

Can anyone think of some other way to use the assert macro for
verification?
What about any other options?

Regards,
Angel Tsankov

推荐答案

Angel Tsankov写道:
Angel Tsankov wrote:
我需要在发布版本中进行验证。如果验证失败,则应显示错误消息,并且程序应该中止。我需要这个解决方案是可移植的。然后我想到了
断言宏 - 它提供了所需的功能,但前提是没有定义NDEBUG。所以,我可以写这样的smth:

#if定义NDEBUG
#undef NDEBUG
#include< cassert>
#define NDEBUG
#否则
#include< cassert>
#endif

不幸的是,NDEBUG可以被定义为有意义的东西,在这种情况下它的价值会丢失。

任何人都可以想到使用断言宏进行
验证的其他方法吗?
其他任何选项怎么样?

问候,
Angel Tsankov
I need to make verifications in release builds. If a verification
fails, an error message should be displayed and the program should be
aborted. I need this solution to be portable. Then I thought of the
assert macro - it provides the desired functionality but only if
NDEBUG is not defined. So, I could write smth like this:

#if defined NDEBUG
#undef NDEBUG
#include <cassert>
#define NDEBUG
#else
#include <cassert>
#endif

Unfortunately, NDEBUG could be defined to something meaningful, in
which case it''s value would be lost.

Can anyone think of some other way to use the assert macro for
verification?
What about any other options?

Regards,
Angel Tsankov




我已经实现了自己的宏,类似于微软的。

类似的东西:


class MyException

{

MyException(const char * msg);

const char * what() const;

};


#define VERIFY(cond)if(cond);否则抛出MyException(#cond)

#if定义(NDEBUG)

#define ASSERT(cond)VERIFY(cond)

#else

#define ASSERT(cond)

#endif


当然,你可以让你的VERIFY做一些事情而不是扔一个

例外。记得尽可能使用编译时断言

而不是运行时断言。


干杯! --M



I have implemented my own macros, which are similar to Microsoft''s.
Something like:

class MyException
{
MyException( const char* msg );
const char* what() const;
};

#define VERIFY(cond) if(cond); else throw MyException( #cond )
#if defined( NDEBUG )
# define ASSERT(cond) VERIFY(cond)
#else
# define ASSERT(cond)
#endif

Of course, you can make your VERIFY do something other than throw an
exception. Remember to use compile-time assertions wherever possible
instead of run-time assertions.

Cheers! --M




" mlimber" <毫升***** @ gmail.com>在消息中写道

news:11 ********************** @ g14g2000cwa.googlegr oups.com ...

"mlimber" <ml*****@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Angel Tsankov写道:
Angel Tsankov wrote:
我需要在发布版本中进行验证。如果验证失败,则应显示错误消息,并且程序应该中止。我需要这个解决方案是可移植的。然后我想到了
断言宏 - 它提供了所需的功能,但前提是没有定义NDEBUG。所以,我可以写这样的smth:

#if定义NDEBUG
#undef NDEBUG
#include< cassert>
#define NDEBUG
#否则
#include< cassert>
#endif

不幸的是,NDEBUG可以被定义为有意义的东西,在这种情况下它的价值会丢失。

任何人都可以想到使用断言宏进行
验证的其他方法吗?
其他任何选项怎么样?

问候,
I need to make verifications in release builds. If a verification
fails, an error message should be displayed and the program should
be
aborted. I need this solution to be portable. Then I thought of the
assert macro - it provides the desired functionality but only if
NDEBUG is not defined. So, I could write smth like this:

#if defined NDEBUG
#undef NDEBUG
#include <cassert>
#define NDEBUG
#else
#include <cassert>
#endif

Unfortunately, NDEBUG could be defined to something meaningful, in
which case it''s value would be lost.

Can anyone think of some other way to use the assert macro for
verification?
What about any other options?

Regards,
Angel Tsankov



我已经实现了自己的宏,类似于微软的。
类似于:

类MyException
{
MyException(const char * msg);
const char * what()const;
};

#define VERIFY(cond)if(条件);否则抛出MyException(#cond)
#if defined(NDEBUG)
#define ASSERT(cond)VERIFY(cond)
#else
#define ASSERT(cond)
#endif

当然,你可以让你的验证做除了抛出
例外之外的事情。



I have implemented my own macros, which are similar to Microsoft''s.
Something like:

class MyException
{
MyException( const char* msg );
const char* what() const;
};

#define VERIFY(cond) if(cond); else throw MyException( #cond )
#if defined( NDEBUG )
# define ASSERT(cond) VERIFY(cond)
#else
# define ASSERT(cond)
#endif

Of course, you can make your VERIFY do something other than throw an
exception.




我想要我的验证显示消息并中止程序。

问题是显示消息的位置 - 将其发送到std :: cerr,使用

消息框,文件还是什么?如果解决方案是便携式的,那也很好。断言宏负责输出消息的位置

并且是可移植的。这就是为什么我在考虑它。然而,正如我提到的那样,它有一个小问题 - 它的价值可能会丢失

当我#undef它。所以,我在寻找如何应对这个问题的想法。我也会感谢任何其他相关的想法。



I want my VERIFY to display a message and abort the program. The
problem is where to display the message - send it to std::cerr, use a
message box, file or what? It would also be nice if the solution is
portable. The assert macro takes care of where to output the message
and is portable. That''s why I''m considering it. However, as I
mentiond, there is a slight problem with it - its value can get lost
when I #undef it. So, I''m asking for ideas how to cope with this
problem. I''d also appreciate any other relevant ideas.


Angel Tsankov写道:
Angel Tsankov wrote:
" mlimber" <毫升***** @ gmail.com>在消息中写道
新闻:11 ********************** @ g14g2000cwa.googlegr oups.com ...
"mlimber" <ml*****@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Angel Tsankov写道:
Angel Tsankov wrote:
我需要在发布版本中进行验证。如果验证失败,则应显示错误消息,并且程序应该中止。我需要这个解决方案是可移植的。然后我想到了
断言宏 - 它提供了所需的功能,但前提是没有定义NDEBUG。所以,我可以写这样的smth:

#if定义NDEBUG
#undef NDEBUG
#include< cassert>
#define NDEBUG
#否则
#include< cassert>
#endif

不幸的是,NDEBUG可以被定义为有意义的东西,在这种情况下它的价值会丢失。

任何人都可以想到使用断言宏进行
验证的其他方法吗?
其他任何选项怎么样?

问候,
I need to make verifications in release builds. If a verification
fails, an error message should be displayed and the program should
be
aborted. I need this solution to be portable. Then I thought of the
assert macro - it provides the desired functionality but only if
NDEBUG is not defined. So, I could write smth like this:

#if defined NDEBUG
#undef NDEBUG
#include <cassert>
#define NDEBUG
#else
#include <cassert>
#endif

Unfortunately, NDEBUG could be defined to something meaningful, in
which case it''s value would be lost.

Can anyone think of some other way to use the assert macro for
verification?
What about any other options?

Regards,
Angel Tsankov



我已经实现了自己的宏,类似于微软的。
类似于:

类MyException
{
MyException(const char * msg);
const char * what()const;
};

#define VERIFY(cond)if(条件);否则抛出MyException(#cond)
#if defined(NDEBUG)
#define ASSERT(cond)VERIFY(cond)
#else
#define ASSERT(cond)
#endif

当然,除了抛出
异常之外,你可以让你的VERIFY做一些事情。



I have implemented my own macros, which are similar to Microsoft''s.
Something like:

class MyException
{
MyException( const char* msg );
const char* what() const;
};

#define VERIFY(cond) if(cond); else throw MyException( #cond )
#if defined( NDEBUG )
# define ASSERT(cond) VERIFY(cond)
#else
# define ASSERT(cond)
#endif

Of course, you can make your VERIFY do something other than throw an
exception.



我希望我的VERIFY显示一个消息并中止该程序。
问题是在哪里显示消息 - 将它发送到std :: cerr,使用
消息框,文件还是什么?如果解决方案是便携式的话也会很好。断言宏负责输出消息的位置,并且是可移植的。这就是为什么我在考虑它。但是,正如我提到的那样,它有一个小问题 - 当我#undef时它的价值可能会丢失。所以,我在寻求如何应对这个问题的想法。我也会感谢任何其他相关的想法。



I want my VERIFY to display a message and abort the program. The
problem is where to display the message - send it to std::cerr, use a
message box, file or what? It would also be nice if the solution is
portable. The assert macro takes care of where to output the message
and is portable. That''s why I''m considering it. However, as I
mentiond, there is a slight problem with it - its value can get lost
when I #undef it. So, I''m asking for ideas how to cope with this
problem. I''d also appreciate any other relevant ideas.




我在上一篇文章中给出了我的相关想法。只需替换你的

所需的行为就可以抛出异常。你可以输出到

std :: cerr并调用std :: exit。


干杯! --M



My relevant idea was given in the previous post. Just substitute your
desired behavior for the throwing of the exception. You could output to
std::cerr and call std::exit, for instance.

Cheers! --M


这篇关于发布构建断言/验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆