Skip to content
This repository was archived by the owner on Jul 3, 2024. It is now read-only.
This repository was archived by the owner on Jul 3, 2024. It is now read-only.

Iterating over std::initializer_list results in a null pointer warning. #92

@roccomoretti

Description

@roccomoretti
#include <initializer_list>

void foo( std::initializer_list< int > const & initlist ) {
        for ( auto const & entry: initlist ) {;}
}

compiled with -Wlifetime results in the following issues

test.cc:5:26: warning: dereferencing a possibly null pointer [-Wlifetime-null]
        for ( auto const & entry: initlist ) {;}
                                ^
test.cc:5:26: note: assigned here
        for ( auto const & entry: initlist ) {;}
                                ^
test.cc:5:8: warning: assigning a possibly null pointer to a non-null object [-Wlifetime-null]
        for ( auto const & entry: initlist ) {;}
              ^~~~~~~~~~~~~~~~~~~

(https://godbolt.org/z/2aBLpr)

The only pointers here that I can think of are the std::initializer_list::begin() and std::initializer_list::end(), and it does look like the cause is thinking that std::initializer_list::begin() can be null:
https://godbolt.org/z/8j_Qs3

(Even if for some reason begin() can return a null pointer, the phrasing of that message when applied to the range-for loop is quite obscure.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions