public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [Patch] Fix variant::operator= on references
@ 2016-09-22  7:46 Tim Shen
  2016-09-22  7:47 ` Tim Shen
  2016-09-22  8:47 ` Jonathan Wakely
  0 siblings, 2 replies; 9+ messages in thread
From: Tim Shen @ 2016-09-22  7:46 UTC (permalink / raw)
  To: libstdc++, gcc-patches; +Cc: Ville Voutilainen

[-- Attachment #1: Type: text/plain, Size: 347 bytes --]

Hi, this patch fixes the following compilation failure:

#include <variant>

int main()
{
   float f1 = 1.0f, f2 = 2.0f;

   std::variant<float&> v1(f1);

   v1 = f2; // #1
}

The bug is caused by a misuse of __storage. I also examined other
__storage usage, they all seem appropriate.

Tested on x86_64-linux-gnu.

Thanks!

-- 
Regards,
Tim Shen

[-- Attachment #2: a.diff --]
[-- Type: application/octet-stream, Size: 1444 bytes --]

commit aa8e2a5a1bb9e615e14fdc8417a60fda5de222a3
Author: Tim Shen <timshen@google.com>
Date:   Thu Sep 22 00:35:13 2016 -0700

    2016-09-22  Tim Shen  <timshen@google.com>
    
    	* libstdc++-v3/include/std/variant (variant::operator=): Fix assignment
    	on references.
    	* libstdc++-v3/testsuite/20_util/variant/compile.cc: Add test.

diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant
index 013884b..911dab3 100644
--- a/libstdc++-v3/include/std/variant
+++ b/libstdc++-v3/include/std/variant
@@ -1147,8 +1147,7 @@ namespace __variant
 	{
 	  constexpr auto __index = __accepted_index<_Tp&&>;
 	  if (index() == __index)
-	    *static_cast<__storage<__to_type<__index>>*>(this->_M_storage())
-	      = forward<_Tp>(__rhs);
+	    std::get<__index>(*this) = forward<_Tp>(__rhs);
 	  else
 	    this->emplace<__index>(forward<_Tp>(__rhs));
 	  __glibcxx_assert(holds_alternative<__accepted_type<_Tp&&>>(*this));
diff --git a/libstdc++-v3/testsuite/20_util/variant/compile.cc b/libstdc++-v3/testsuite/20_util/variant/compile.cc
index 99f980a..a0a8d70 100644
--- a/libstdc++-v3/testsuite/20_util/variant/compile.cc
+++ b/libstdc++-v3/testsuite/20_util/variant/compile.cc
@@ -169,6 +169,12 @@ void copy_assign()
     variant<DefaultNoexcept> a;
     static_assert(!noexcept(a = a), "");
   }
+
+  {
+    float f1 = 1.0f, f2 = 2.0f;
+    std::variant<float&> v1(f1);
+    v1 = f2;
+  }
 }
 
 void move_assign()

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

end of thread, other threads:[~2016-09-22 10:59 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-09-22  7:46 [Patch] Fix variant::operator= on references Tim Shen
2016-09-22  7:47 ` Tim Shen
2016-09-22  8:47 ` Jonathan Wakely
2016-09-22  8:50   ` Tim Shen
2016-09-22 10:07     ` Jonathan Wakely
2016-09-22 10:12       ` Ville Voutilainen
2016-09-22 10:40       ` Tim Shen
2016-09-22 10:59         ` Tim Shen
2016-09-22 11:18           ` Jonathan Wakely

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).