[cfe-dev] Inconsistent Wdangling-gsl warning
Csaba Raduly via cfe-dev
cfe-dev at lists.llvm.org
Fri Jun 25 01:51:15 PDT 2021
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> >)
https://godbolt.org/z/5erq8Mh7W
However, only line 9 gets a Wdangling-gsl warning. Shouldn't line 7 get one too?
Csaba
--
You can get very substantial performance improvements
by not doing the right thing. - Scott Meyers, An Effective C++11/14 Sampler
So if you're looking for a completely portable, 100% standards-conformant way
to get the wrong information: this is what you want. - Scott Meyers (C++TDaWYK)
More information about the cfe-dev
mailing list