public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "danakj at orodu dot net" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/110158] Cannot use union with std::string inside in constant expression
Date: Wed, 06 Sep 2023 18:18:48 +0000	[thread overview]
Message-ID: <bug-110158-4-y2zEbegfN9@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-110158-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #6 from danakj at orodu dot net ---
In case it is of help, here's an even smaller repro that clang reports the
error in libstdc++. For whatever reason gcc does not notice the libstdc++ bug
here.

https://gcc.godbolt.org/z/Phndafeoe

```
#include <string>

struct S {
    constexpr ~S() {}

    std::string s;
};

constexpr std::string foo(const S& s) { return s.s; }

static_assert(foo(S("hi")) == "hi");  // Fails.
static_assert(foo(S("a longer string works")) == "a longer string works");

int main() {}
```

The error:
```
<source>:11:15: error: static assertion expression is not an integral constant
expression
static_assert(foo(S("hi")) == "hi");
              ^~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/gcc-12.2.0/lib/gcc/x86_64-linux-gnu/12.2.0/../../../../include/c++/12.2.0/bits/basic_string.h:356:10:
note: assignment to member '_M_local_buf' of union with no active member is not
allowed in a constant expression
            __c = _CharT();
                ^
/opt/compiler-explorer/gcc-12.2.0/lib/gcc/x86_64-linux-gnu/12.2.0/../../../../include/c++/12.2.0/bits/basic_string.tcc:229:4:
note: in call to '&S(("hi")).s->_M_use_local_data()'
          _M_use_local_data();
          ^
/opt/compiler-explorer/gcc-12.2.0/lib/gcc/x86_64-linux-gnu/12.2.0/../../../../include/c++/12.2.0/bits/basic_string.h:642:2:
note: in call to '&S(("hi")).s->_M_construct(&"hi"[0], &"hi"[2], {{}})'
        _M_construct(__s, __end, forward_iterator_tag());
        ^
<source>:11:21: note: in call to 'basic_string(&"hi"[0],
std::allocator<char>())'
static_assert(foo(S("hi")) == "hi");
                    ^
1 error generated.
```

  parent reply	other threads:[~2023-09-06 18:18 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-07 15:22 [Bug libstdc++/110158] New: " 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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-110158-4-y2zEbegfN9@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).