Re: g++ 7.3.0 & 8.2.0: weird std::string_view behavior when using in-line if-else

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, 5 Oct 2018, Tmplt wrote:

today some code of mine using std::string_view broke out of seemingly
nowhere; the following program is a minimal recreation of the odd
behavior I'm seeing:

	$ cat string_view_test.cpp
	#include <string_view>
	#include <string>
	#include <iostream>

	int main()
	{
		std::string str = "Victory of Eagles";
		std::string_view sv = true ? str : "";

What do you think is the type of true?str:"" ? It is going to be std::string (prvalue), you would need both arms to be the same type to get an lvalue. Your string_view thus refers to a temporary copy of str, which gets destructed at the end of the line.

		std::cout << sv << "\n";
	}

The above program, when compiled with g++ v7.3.0 or v8.2.0 via

	$ g++ -std=c++17 string_view_test.cpp

prints "of Eagles" when run where "Victory of Eagles" is expected.
Omitting the in-lined if-else, and instead just

	std::string_view sv = str;

solves the issue, but I have no idea why. What's up with this behavior?

--
Marc Glisse



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux