2011-12-13 142 views
0

可能重复使用:
What is the purpose of the statement “(void)c;”?是什么`(无效)的D'

class ArgString : public Arg::Base 
{ 
public: 
    ... 
    bool CanConvertToInt() const 
    { 
     const char *cstr = mValue.c_str(); 
     char *result = 0; 
     long d = strtol(cstr, &result, 10); 
     (void) d; // what is the usage of this line? 
     return result != cstr; 
    } 

private: 
    std::string mValue; 
}; 

谁能告诉我下面的行的目的是什么?

(void) d; 

谢谢

// //更新

正如一些人指出,该行的目的是抑制编译警告。对我来说,这很奇怪。因为这是非常严重的警告

warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use 
_CRT_SECURE_NO_WARNINGS. See online help for details. 

为什么我们忽略这个大的警告,并只处理较小的警告。

+0

我不知道目的是什么,但它恕我直言,声明不产生任何效果;-) – emesx 2011-12-13 16:59:05

+0

请注意,这是你有控制权的代码,请评论答案后。 – 111111 2011-12-13 16:59:35

回答

4

模式(void)d通常会告诉代码分析器您明确忽略函数的返回值。许多C分析人员认为忽略返回值是错误的,因为它可能导致忽略故障。这是一种说“我打算这么做”的方式。

3

我唯一见过类似的东西是为了防止对未使用变量的警告。将一些东西投射到(void)绝对没有什么,但它算作变量的用法。

1

它避免了未使用的变量警告。我已经看到这用于声明宏,以便在发布时不会得到未使用的变量警告。它评估为无操作。