Consider the following snippet:
#include <string>#include <string_view>int main() { auto str = std::string{}; auto sv1 = std::string_view(str +"!"); // <- warning :) std::string_view sv2(str +"!"); // <- warning :) std::string_view sv3 = str +"!"; // <- warning :) auto sv4 = std::string_view{str +"!"}; // <- no warning :( std::string_view sv5{str +"!"}; // <- no warning :(}
Compiled with flags -std=c++17 -Wdangling-gsl
. Tested in clang 12.0.1 and trunk in compiler explorer.
As expected, in sv1
, sv2
and sv3
, str +"!"
triggers the warning:
Object backing the pointer will be destroyed at the end of the full-expression [-Wdangling-gsl]
However in sv4
and sv5
the same expression, but using {}
, does not.
Is this an expected behaviour of using {}
? Is this a bug in clang? Am I just missing something?