public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "barry.revzin at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/109890] New: vector's constructor doesn't start object lifetimes during constant evaluation
Date: Wed, 17 May 2023 13:50:22 +0000	[thread overview]
Message-ID: <bug-109890-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 109890
           Summary: vector's constructor doesn't start object lifetimes
                    during constant evaluation
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: barry.revzin at gmail dot com
  Target Milestone: ---

From StackOverflow (https://stackoverflow.com/q/76269606/2069064), clang
rejects this code when compiling with libstdc++:

#include <vector>

consteval auto bar(int n){
    std::vector<int> v(n);
    return v[0];
}
constexpr auto m = bar(5);

This is because libstdc++ basically does something like this:

#include <memory>

class V {
    int* p;
    int n;
    std::allocator<int> alloc;

public:
    constexpr V(int n)
        : n(n)
    {
        p = alloc.allocate(n);

        // fill with 0s?
        for (int i = 0; i != n; ++i) {
            p[i] = 0;
        }
    }

    constexpr ~V() {
        alloc.deallocate(p, n);
    }
};

consteval auto bar(int n) {
    V v(n);
    return n;
}
static_assert(bar(5) == 5);

And clang is more picky about the assignment there - it doesn't like just
writing p[0] = 0, because the int's lifetime hasn't started yet. gcc accepts
the above though. 

I think that's... technically correct (if pedantic) and libstdc++'s path needs
to do a construct_at somewhere.

             reply	other threads:[~2023-05-17 13:50 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-17 13:50 barry.revzin at gmail dot com [this message]
2023-05-17 14:06 ` [Bug libstdc++/109890] " 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-109890-4@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).