http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50276

             Bug #: 50276
           Summary: Wrong "used uninitialized in this function" warning
                    [C++0x]
    Classification: Unclassified
           Product: gcc
           Version: 4.6.1
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c++
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: bisq...@iki.fi


For this example code, GCC mistakenly produces the following warning:
tmp.cc:10:5: warning: 'value' is used uninitialized in this function
[-Wuninitialized]
The warning is wrongly given, because there is no execution path that does not
assign a well-defined value to the variable. In fact, there are no branches at
all between the declaring and the assigning of the variable.

template<typename T>
unsigned testfun(const T& func)
{
    return func();
}

template<int i>
unsigned test()
{
    if(unsigned value = testfun( [] () { return 0; }))
    {
        return value;
    }
    return i;
}

int main()
{
    return test<1>();
}

The warning being wrongly given depends on the following conditions:
- "test()" being a template function: changing "i" into an actual parameter
removes the warning
- "func" being a functor: changing it into an integer parameter removes the
warning
- the variable "value" being declared and assigned to in the if-condition:
declaring and assigning it separately removes the warning.
- the "func" parameter being a lambda function: changing it into a static
method of a class removes the warning.

The following aspects do not affect the warning:
- "testfun()" being a template function: changing "T" into an explicit int(*)()
retains the warning
- whether "i" is used within "test()" or not
- adding "static" or "inline" attributes to any function did not change the
warning.

Tested on GCC 4.5.3 and GCC 4.6.1, on x86_64-linux-gnu in both 32-bit and
64-bit mode on all optimization modes.

Reply via email to