Let's see this code pattern I'm seeing often:
struct Foo
{
template <typename T>
T* as1() { /* ... */ }
template <typename T>
T* as2(T*) { /* ... */ }
};
The former method is to be used like this:
SomeComplexTypeAndNotAuto * a = foo.as1<SomeComplexTypeAndNotAuto>();
While the latter is more convenient to use since you don't need to repeat the complex type:
SomeComplexTypeAndNotAuto * a = foo.as2(a);
However, most compiler rejects the 2nd case with a Wuninitialized warning:
warning: variable 'a' is uninitialized when used within its own initialization [-Wuninitialized]
It's quite clear the variable is not used in the initialization, only its type is. Is there a way to avoid this warning without dealing with the hell of per-compiler pragma ?
Edit:
It's not clear from my initial post, but when I wrote SomeComplexTypeNotAuto
, I meant that such code like this:
auto a = foo.as2(a);
can not be resolved since you have to provide one type to allow the compiler to deduce it.
My question was specific to the fact that method as2()
is a template, as as such must be visible at specialization time for a type T
. So the compiler can see that the argument T*
does not even have a name so it's not used/usable from within the function. As such, I don't get why it warns about the "unused variable" warning since it's clear it's not used.