public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/110158] New: Cannot use union with std::string inside in constant expression
@ 2023-06-07 15:22 fchelnokov at gmail dot com
  2023-06-07 19:40 ` [Bug c++/110158] " pinskia at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: fchelnokov at gmail dot com @ 2023-06-07 15:22 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 110158
           Summary: Cannot use union with std::string inside in constant
                    expression
           Product: gcc
           Version: 13.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: fchelnokov at gmail dot com
  Target Milestone: ---

This program

constexpr bool f() {
    union U{
        std::string s;
        constexpr ~U(){ s.~basic_string(); }
    } u{};
    return true;
}

static_assert( f() );

is accepted in MSVC and Clang with libc++. But in GCC it produces the error:

error: non-constant condition for static assertion
   in 'constexpr' expansion of 'f()'
   in 'constexpr' expansion of '(& u)->f()::U::~U()'
   in 'constexpr' expansion of
'((f()::U*)this)->f()::U::s.std::__cxx11::basic_string<char>::~basic_string()'
/opt/compiler-explorer/gcc-13.1.0/include/c++/13.1.0/bits/basic_string.h:803:19:
  in 'constexpr' expansion of
'((std::__cxx11::basic_string<char>*)this)->std::__cxx11::basic_string<char>::_M_dispose()'
error: accessing 'std::__cxx11::basic_string<char>::<unnamed
union>::_M_allocated_capacity' member instead of initialized
'std::__cxx11::basic_string<char>::<unnamed union>::_M_local_buf' member in
constant expression

Online demo: https://gcc.godbolt.org/z/bbf4Yo3v9

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

end of thread, other threads:[~2023-11-22 11:54 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-07 15:22 [Bug libstdc++/110158] New: Cannot use union with std::string inside in constant expression fchelnokov at gmail dot com
2023-06-07 19:40 ` [Bug c++/110158] " pinskia at gcc dot gnu.org
2023-08-03 17:11 ` ppalka at gcc dot gnu.org
2023-08-04 15:55 ` pinskia at gcc dot gnu.org
2023-08-04 16:16 ` danakj at orodu dot net
2023-09-06 18:11 ` danakj at orodu dot net
2023-09-06 18:18 ` danakj at orodu dot net
2023-09-29 11:08 ` redi at gcc dot gnu.org
2023-11-22  0:45 ` ppalka at gcc dot gnu.org
2023-11-22 11:54 ` redi 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).