public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/50276] New: Wrong "used uninitialized in this function" warning [C++0x]
@ 2011-09-02 12:45 bisqwit at iki dot fi
  2011-09-02 13:05 ` [Bug c++/50276] " bisqwit at iki dot fi
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: bisqwit at iki dot fi @ 2011-09-02 12:45 UTC (permalink / raw)
  To: gcc-bugs

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: unassigned@gcc.gnu.org
        ReportedBy: bisqwit@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.


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2013-03-05 18:59 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-09-02 12:45 [Bug c++/50276] New: Wrong "used uninitialized in this function" warning [C++0x] bisqwit at iki dot fi
2011-09-02 13:05 ` [Bug c++/50276] " bisqwit at iki dot fi
2011-09-14 21:11 ` [Bug c++/50276] [C++0x] Wrong "used uninitialized in this function" warning paolo.carlini at oracle dot com
2012-01-03 22:46 ` paolo.carlini at oracle dot com
2012-01-03 23:02 ` paolo.carlini at oracle dot com
2012-01-03 23:16 ` bisqwit at iki dot fi
2013-03-05 17:42 ` jason at gcc dot gnu.org
2013-03-05 18:59 ` jason at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).