public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/113294] New: constexpr error from accessing inactive union member in basic_string after move assignment
@ 2024-01-09 17:04 pkeir at outlook dot com
  2024-01-10  0:25 ` [Bug libstdc++/113294] " pinskia at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: pkeir at outlook dot com @ 2024-01-09 17:04 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113294

            Bug ID: 113294
           Summary: constexpr error from accessing inactive union member
                    in basic_string after move assignment
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pkeir at outlook dot com
  Target Milestone: ---

The C++20 program below fails to compile. A call to `basic_string::clear()` in
the move assignment operator leads to an access of inactive union member
`_M_local_buf`.

#include <string>

constexpr bool string_test1()
{
  {
    std::string str1; //  = "1"; // also a problem when small (less than 16)
    std::string str2 = "1234567890123456"; // 16 chars
    str1 = std::move(str2);
  }

  return true;
}

static_assert(string_test1());

Changing `__str._M_data(__str._M_local_buf);` to
`__str._M_data(__str._M_use_local_data());` in basic_string.h seems to fix it,
though I've not had time to test it fully. With this solution, the function
below also remains functional:

constexpr bool string_test2()                                                   
{                                                                               
  {                                                                             
    std::string str = "1234567890123456";  // 16 chars                          
    str = std::string{"1234567890123456"}; // rvalue assignment                 
  }                                                                             

  return true;                                                                  
}

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

end of thread, other threads:[~2024-02-16 15:31 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-09 17:04 [Bug libstdc++/113294] New: constexpr error from accessing inactive union member in basic_string after move assignment pkeir at outlook dot com
2024-01-10  0:25 ` [Bug libstdc++/113294] " pinskia at gcc dot gnu.org
2024-01-23 16:45 ` ppalka at gcc dot gnu.org
2024-02-12 23:19 ` cvs-commit at gcc dot gnu.org
2024-02-16 15:21 ` cvs-commit at gcc dot gnu.org
2024-02-16 15:31 ` ppalka at gcc dot gnu.org

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