#include <string>
using namespace std;
int main()
{
string s; // no warning
int i; // warning C4101
return 0;
}
i
but not about s
in the example?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
}
Comparison of compilers' treatment
As can be observed, they are consistent.
Since std::string
cannot possibly be trivially destructible, the compilers won't warn about it.
So to answer your question: you can't.
There is no warning because actually there is no unused variable s
. s
is an instance of the string
class and this class has a constructor which is called upon the declaration string s;
, therefore s
is used by it's constructor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With