public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "miro.palmu at helsinki dot fi" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/113021] New: [constexpr] gcc rejects initializing struct containing vector during constant evaluation depending if the struct also contains other member
Date: Thu, 14 Dec 2023 14:25:51 +0000	[thread overview]
Message-ID: <bug-113021-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 113021
           Summary: [constexpr] gcc rejects initializing struct containing
                    vector during constant evaluation depending if the
                    struct also contains other member
           Product: gcc
           Version: 13.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: miro.palmu at helsinki dot fi
  Target Milestone: ---

Following code fails to compile on gcc (GCC) 13.2.1 20230801 but on clang
(16.0.6 or 17.0.1). 

```
#include <vector>

struct A {
  std::vector<int> vec;
  int a;
};

consteval auto foo() { const A tmp1{{1, 2}, 42}; }

int main() {
  foo();
  return 0;
}

```

With following error message:

```
> g++ -Wall -Wextra -std=c++20 main.cpp
In file included from /usr/include/c++/13.2.1/vector:66,
                 from main2.cpp:1:
/usr/include/c++/13.2.1/bits/stl_vector.h: In function ‘int main()’:
main2.cpp:11:6:   in ‘constexpr’ expansion of ‘foo()’
main2.cpp:8:49:   in ‘constexpr’ expansion of
‘std::vector<int>(std::initializer_list<int>{((const int*)(& const int [2]{1,
2})), 2}, ((const std::vector<int>::allocator_type&)(&
std::allocator<int>())))’
/usr/include/c++/13.2.1/bits/stl_vector.h:679:21:   in ‘constexpr’ expansion of
‘((std::vector<int>*)this)->std::vector<int>::_M_range_initialize<const
int*>(__l.std::initializer_list<int>::begin(),
__l.std::initializer_list<int>::end(),
(std::random_access_iterator_tag{std::bidirectional_iterator_tag()},
std::forward_iterator_tag()))’
/usr/include/c++/13.2.1/bits/stl_vector.h:1689:13: error: modifying a const
object
‘((std::vector<int>*)this)->std::vector<int>::<anonymous>.std::_Vector_base<int,
std::allocator<int> >::_M_impl.std::_Vector_base<int, std::allocator<int>
>::_Vector_impl::<anonymous>.std::_Vector_base<int, std::allocator<int>
>::_Vector_impl_data::_M_start’ is not allowed in a constant expression
 1688 |           this->_M_impl._M_start
      |           ~~~~~~~~~~~~~~~~~~~~~~
 1689 |             = this->_M_allocate(_S_check_init_len(__n,
_M_get_Tp_allocator()));
      |            
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main2.cpp:8:32: note: originally declared ‘const’ here
    8 | consteval auto foo() { const A tmp1{ {1, 2}, 42 }; }
      |                                ^~~~
```

If member variable `a` is removed it compiles:

```
#include <vector>

struct A {
  std::vector<int> vec;
};

consteval auto foo() { const A tmp1{ {1, 2} }; }

int main() {
  foo();
  return 0;
}
```

             reply	other threads:[~2023-12-14 14:25 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-14 14:25 miro.palmu at helsinki dot fi [this message]
2023-12-14 17:35 ` [Bug c++/113021] " pinskia at gcc dot gnu.org
2023-12-15 10:44 ` miro.palmu at helsinki dot fi
2023-12-15 10:55 ` miro.palmu at helsinki dot fi
2023-12-15 19:40 ` pinskia 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-113021-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).