Wunused,但设置变量报警处理 [英] Wunused-but-set-variable warning treatment

查看:1037
本文介绍了Wunused,但设置变量报警处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code,并同时用gcc-4.6编译它,我获得警告:


  

警告:变量状态设置,但不使用[-Wunused,但是设置可变]


 #如果定义(_DEBUG_)
的#define ASSERT断言
的#else / * * _DEBUG_ /
的#define ASSERT(__exp__)
#万一静态内嵌无效cl_plock(cl_plock_t * const的p_lock)
{
        status_t状态;
        ASSERT(p_lock);
        ASSERT(p_lock->状态==初始化);        状态= pthread_rwlock_unlock(安培; p_lock->锁);
        ASSERT(状态== 0);
}

在_DEBUG_
标志未设置我得到的警告。
任何想法我怎么能解决这个警告?


解决方案

您可以将您的 ASSERT 宏更改为:

 #如果定义(_DEBUG_)
的#define ASSERT断言
的#else / * * _DEBUG_ /
的#define ASSERT(EXP)((无效)(EXP))
#万一

如果除权pression没有sideeffects,那么它仍然应该优化掉了,但也要燮preSS警告(如前pression的确实的有副作用,那么你将得到调试和非调试版本不同的结果,你不想要么!)。

I have the following code, and while compiling it with gcc-4.6 I get warning:

warning: variable ‘status’ set but not used [-Wunused-but-set-variable]

#if defined (_DEBUG_)
#define ASSERT       assert
#else                           /* _DEBUG_ */
#define ASSERT( __exp__ )
#endif   

static inline void cl_plock(cl_plock_t * const p_lock)
{
        status_t status;
        ASSERT(p_lock);
        ASSERT(p_lock->state == INITIALIZED);

        status = pthread_rwlock_unlock(&p_lock->lock);
        ASSERT(status == 0); 
}

When _DEBUG_ flag isn't set I get the warning. Any ideas how can I workaround this warning?

解决方案

You can change your ASSERT macro to:

#if defined (_DEBUG_)
#define ASSERT       assert
#else                           /* _DEBUG_ */
#define ASSERT( exp ) ((void)(exp))
#endif   

If the expression has no sideeffects, then it should still be optimised out, but it should also suppress the warning (if the expression does have side-effects, then you would get different results in debug and non-debug builds, which you don't want either!).

这篇关于Wunused,但设置变量报警处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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