GCC compiler warnings with Optimized flag

A quick question on building with the -o (optimized flag).

I have the following basic template functions defined:

template CLASS_ACTION_PARAM_TYPE_ID paramType(void)
{
T tmp;
return paramType(tmp);
}

template CLASS_ACTION_PARAM_TYPE_ID paramType(T value)
{
if (typeid(value) == typeid(long)) return PARAM_TYPE_LONG;
if (typeid(value) == typeid(unsigned long)) return PARAM_TYPE_ULONG;
if (typeid(value) == typeid(int)) return PARAM_TYPE_INT;
if (typeid(value) == typeid(unsigned int)) return PARAM_TYPE_UINT;
if (typeid(value) == typeid(short)) return PARAM_TYPE_SHORT;
if (typeid(value) == typeid(unsigned short)) return PARAM_TYPE_USHORT;
if (typeid(value) == typeid(char)) return PARAM_TYPE_CHAR;
if (typeid(value) == typeid(unsigned char)) return PARAM_TYPE_UCHAR;
if (typeid(value) == typeid(float)) return PARAM_TYPE_FLOAT;
if (typeid(value) == typeid(double)) return PARAM_TYPE_DOUBLE;
if (typeid(value) == typeid(string)) return PARAM_TYPE_STRING;
if (typeid(value) == typeid(long long)) return PARAM_TYPE_LONG64;
if (typeid(value) == typeid(unsigned long long)) return PARAM_TYPE_ULONG64;

return PARAM_TYPE_UNKNOWN;

}

The only purpose is to return an enumerated type based on the type of variable that is used (yes, it could have been a case statement but I didn’t write the code and don’t want to change it now if I don’t have to).

Anyway I’m trying to compile with the optimize flag. gcc errrors out on:

T tmp;

saying this could be used uninitialized. Obviously it’s not a real error but I want to stop the compiler complaining about it so I can build an optimized version of the code. However I can’t see anyway to provide a default initial value since the T could be an long, string, short etc.

I considered doing:

#pragma warning (disable:xxxx)
T tmp;
#pragma warning (enable:xxxx)

But I have no idea how to figure out what xxxx is (the warning number) because gcc doesn’t provide one and no place on the web seems to have a listing of all the warning pragma numbers.

So can this bit of code be made to compile with no warnings or am I stuck doing a re-write of the code into a case statement?

Thanks,

Tim

I tried your code on my system and it didn’t fail. Can you mail me the (preprocessed) file?

Colin,

I couldn’t find your Email address in your user profile. So I am replying here with the attachment.

Instead of the pre-processed file I am giving you a nice small tar achive of some header files and one source file that you can use to generate the problem. It’s non-trivial C++ code which is why I could not construct a simple example to send.

This is the compile line I used from the command prompt:

qcc -V3.3.1,gcc_ntox86_cpp -lang-c++ -O -D_PTHREADS=1 -w8 -Wall
-Werror -Wno-deprecated Action.cpp

This is the output I get:

DeviceActionParameterID.h: In function Hardware::CLASS_ACTION_PARAM_TYPE_ID
Hardware::paramType() [with T = long int]’:
DeviceActionParameterID.h:65: warning: long int tmp’ might be used
uninitialized in this function cc:
/usr/qnx630/host/qnx6/x86/usr/lib/gcc-lib/i386-pc-nto-qnx6.3.0/3.3.1/cc1plus error 1

Tim

Crappy site won’t take a file bigger than 256K so this is coming in several archives :slight_smile:

Tim

And on…

and on some more…

and here is the last one.

Tim

I’m not a C++ guru, but making

T tmp;

into

T tmp();

seems to silence the warning…

Colin,

This works at compilation time but at link time it assmed tmp() is a function call and so the linker gets an undefined reference to tmp() :slight_smile:

So it’s back to needing to either wrapping this in a praga to disable the check around that variable or else I will have to end up rewriting the template class into specialized functions for each of the data types (which I’d prefer not to do if I can avoid)

Tim

template CLASS_ACTION_PARAM_TYPE_ID paramType(void)
{
return paramType(T());
}

That’s what I was meaning to do - does it work?

Yes it works :slight_smile:

Thanks very much guys.

One question though. This solution just removes the local variable so the problem goes away. If I did need one, is it possible to create an initialized local variable in a template class like this?

I understand that T() is calling the constructor of the simple variable types (int, float etc) and that when I did 'T tmp(); ’ the compiler got confused and assumed I was making a function call to tmp instead of doing a constructor call.

Tim

T tmp = T();

seems to work, but I have no clue as to whether it’s correct :v)