public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* g++ 7.3.0 & 8.2.0: weird std::string_view behavior when using in-line if-else
@ 2018-10-05  0:53 Tmplt
  2018-10-05  6:16 ` Marc Glisse
  0 siblings, 1 reply; 2+ messages in thread
From: Tmplt @ 2018-10-05  0:53 UTC (permalink / raw)
  To: gcc-help

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.

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: g++ 7.3.0 & 8.2.0: weird std::string_view behavior when using in-line if-else
  2018-10-05  0:53 g++ 7.3.0 & 8.2.0: weird std::string_view behavior when using in-line if-else Tmplt
@ 2018-10-05  6:16 ` Marc Glisse
  0 siblings, 0 replies; 2+ messages in thread
From: Marc Glisse @ 2018-10-05  6:16 UTC (permalink / raw)
  To: Tmplt; +Cc: gcc-help

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-10-05  6:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-10-05  0:53 g++ 7.3.0 & 8.2.0: weird std::string_view behavior when using in-line if-else Tmplt
2018-10-05  6:16 ` Marc Glisse

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).