public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "benoit.labrique at endress dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/111737] New: Object holding a pointer to an uninitialized c-array not usable in a constant expression
Date: Mon, 09 Oct 2023 12:24:23 +0000	[thread overview]
Message-ID: <bug-111737-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 111737
           Summary: Object holding a pointer to an uninitialized c-array
                    not usable in a constant expression
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: benoit.labrique at endress dot com
  Target Milestone: ---

Following code does not compile (see details below) as soon as there is an
indirection, in this case the function MakeBuffer().

This kind of construct is very common in the embedded world, where dynamic
memory allocation is not permitted. As in the code example, buffers of
different size can be passed to more generic functionality via a reference to
the base class "Collection", which is buffer-size agnostic.

// Compiled with -O2 -std=c++17

class Collection
{
protected:
    constexpr Collection(void * iBuffer)
        : m_buffer(iBuffer)
    {
    }

protected:
    void * m_buffer = nullptr;
};

class Buffer final : public Collection
{
public:
    explicit constexpr Buffer()
        : Collection(m_buffer), m_buffer{0} // gcc doesn't like that m_buffer
is used before it is initialized. Using std::array instead won't work at all.
    {
    }

private:
    int    m_buffer[1];
};

namespace
{
    inline constexpr auto MakeBuffer()
    {
        return Buffer();    // C++17: shall definitively perform copy elision
    }

    constexpr Buffer VIA_MAKE_BUFFER = MakeBuffer();        // Only gcc has a
problem with, neither clang nor msvc
    constexpr Buffer VIA_BUFFER_CONSTRUCTOR = Buffer();     // Why is it now OK
for gcc here?
}

int main()
{
    constexpr auto a = VIA_MAKE_BUFFER;
    constexpr auto b = VIA_BUFFER_CONSTRUCTOR;
    return 0;
}

https://godbolt.org/z/erPsooP4b

Compiling this code issues (ARM GCC 13.2 (or earlier)):

<source>: In function 'int main()':
<source>:38:24: error: the value of '{anonymous}::VIA_MAKE_BUFFER' is not
usable in a constant expression
   38 |     constexpr auto a = VIA_MAKE_BUFFER;
      |                        ^~~~~~~~~~~~~~~
<source>:32:22: note: '{anonymous}::VIA_MAKE_BUFFER' used in its own
initializer
   32 |     constexpr Buffer VIA_MAKE_BUFFER = MakeBuffer();        // Only gcc
has a problem with, neither clang nor msvc
      |                      ^~~~~~~~~~~~~~~

Alternatively: on x86-64 GCC 13.2 (or earlier):

<source>:32:51: error:
'Buffer{Collection{((void*)(&<anonymous>.Buffer::m_buffer))}, int [1]{0}}' is
not a constant expression
   32 |     constexpr Buffer VIA_MAKE_BUFFER = MakeBuffer();        // Only gcc
has a problem with, neither clang nor msvc
      |

                 reply	other threads:[~2023-10-09 12:24 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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