mardi 24 mars 2020

Is this an incorrect warning?

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 ?

Aucun commentaire:

Enregistrer un commentaire