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

Tmplt tmplt@dragons.rocks
Fri Oct 5 00:53:00 GMT 2018


Hello,

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 : "";
		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?

Cheers.



More information about the Gcc-help mailing list