如何使Visual Studio 2010警告未使用的变量? [英] How to make Visual Studio 2010 warn about unused variables?

查看:114
本文介绍了如何使Visual Studio 2010警告未使用的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <string>

using namespace std;

int main()
{
    string s; // no warning
    int i;    // warning C4101

    return 0;
}




  1. 为什么Visual Studio会警告我有关未使用的内容变量 i 但在示例中不是大约 s 吗?

  2. 我假设编译器不确定字符串构造函数的副作用。这是不显示警告的原因吗?

  3. 我能以某种方式启用有关未使用的字符串变量的警告吗?

  1. Why does Visual Studio warn me about the unused variable i but not about s in the example?
  2. I assume that the compiler is not sure about side effects of the string constructor. Is this the reason for not showing a warning?
  3. Can I somehow enable warnings about unused string variables?

我的警告级别设置为4。

My warning level is set to 4.

推荐答案

我假设编译器仅警告琐碎可构造的未使用变量/

I hypothesize that compilers only warn about unused variables for trivially constructible/destructible types.

template<typename>
struct Empty
{

};

template<typename T>
struct Trivial : Empty<T>
{
    int* p;
    int i;
};

template<typename>
struct NonTrivial
{
    NonTrivial() {}
};

template<typename>
struct TrivialE
{
    TrivialE& operator=(const TrivialE&) {}
};

struct NonTrivial2
{
    NonTrivial2() {}
};

struct NonTrivialD
{
    ~NonTrivialD() {}
};

int main()
{
    Empty<int> e;      // warning
    Trivial<int> t;    // warning
    NonTrivial<int> n; // OK
    TrivialE<int> te;  // warning
    NonTrivial2 n2;    // OK
    NonTrivialD nd;    // OK
}

比较编译器的处理方式

可以看出,它们是一致的。

As can be observed, they are consistent.

由于 std :: string 不可能被轻易破坏,因此编译器不会对此发出警告。

Since std::string cannot possibly be trivially destructible, the compilers won't warn about it.

要回答您的问题:您不能。

So to answer your question: you can't.

这篇关于如何使Visual Studio 2010警告未使用的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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