Inconsistent Wdangling-gsl warning

Hi all,

The following code:

#include <string>
#include <string_view>
#include <iostream>

int main()
{
    std::string_view sv1 = std::string_view{std::string{"string1"}};
    std::cout << sv1;
    std::string_view sv2 = std::string_view(std::string{"string2"}); // line 9
    std::cout << sv2;
}

generates the following sequence of calls:

call std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >::basic_string<std::allocator<char> >(char
const*, std::allocator<char> const&)
call std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >::operator std::basic_string_view<char,
std::char_traits<char> >() const
call std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >::~basic_string() [complete object destructor]
call std::basic_ostream<char, std::char_traits<char> >&
std::operator<< <char, std::char_traits<char>

(std::basic_ostream<char, std::char_traits<char> >&,

std::basic_string_view<char, std::char_traits<char> >)
call std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >::basic_string<std::allocator<char> >(char
const*, std::allocator<char> const&)
call std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >::operator std::basic_string_view<char,
std::char_traits<char> >() const
call std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >::~basic_string() [complete object destructor]
call std::basic_ostream<char, std::char_traits<char> >&
std::operator<< <char, std::char_traits<char>

(std::basic_ostream<char, std::char_traits<char> >&,

std::basic_string_view<char, std::char_traits<char> >)

However, only line 9 gets a Wdangling-gsl warning. Shouldn't line 7 get one too?

Csaba

Hi Csaba,

I do agree it seems to be a false negative. I plan to look into this at some point but no promises when.

Cheers,
Gabor