Defined but not used? But I am using it!

T

Travis

I can't figure this out. So in a templated class of mine I have the
following.

namespace menutree
{
static bool debug = false;
}


And in various parts of the code I have statements like.

if (menutree::debug) { std::cout << "here" << std::endl;

Everything compiles but I get the warning

"warning: `bool menutree::debug' defined but not used"

Can someone shed some light on this?

Thanks!
 
V

Victor Bazarov

Travis said:
I can't figure this out. So in a templated class of mine I have the
following.

namespace menutree
{
static bool debug = false;
}

You have a namespace _in_ "a templated class"? How so?
And in various parts of the code I have statements like.

if (menutree::debug) { std::cout << "here" << std::endl;

Everything compiles but I get the warning

"warning: `bool menutree::debug' defined but not used"

Can someone shed some light on this?

I think this is covered by the FAQ 5.8.

V
 
T

Travis

You have a namespace _in_ "a templated class"? How so?






I think this is covered by the FAQ 5.8.

V

Sorry not IN the template, in the template .h file but outside the
template's definition.

FAQ 5.8?
 
Z

Zeppe

Travis said:
Sorry not IN the template, in the template .h file but outside the
template's definition.

FAQ 5.8?

aka "too little code to help". Perhaps you use the variable in a
template that is never instantiated?

Regards,

Zeppe
 
M

Markus Schoder

Sorry not IN the template, in the template .h file but outside the
template's definition.

Since "debug" is static you get a new instance for each translation unit
this header file is included in (directly or indirectly). Now there
probably is a translation unit where you do not use "debug" and that is
where the compiler complains since you have a static variable that is not
used in a given translation unit and since it cannot be used in a
different translation unit that does not seem to make sense.

Assuming you do not want to change the value of "debug" at run-time try
to declare it as

namespace menutree
{
const bool debug = false;
}

This allows for better optimization and hopefully silences the compiler
as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top