https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125008
Bug ID: 125008
Summary: array member with consteval copy constructor generates
missing symbol
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: marcel at laverdet dot com
Target Milestone: ---
// a.cc
struct consteval_copy {
consteval_copy() = default;
consteval consteval_copy(const consteval_copy&) {};
};
struct array_of {
consteval_copy a[ 1 ];
};
constexpr auto array_value = array_of{};
template <class>
struct copy_of : array_of {
constexpr copy_of() : array_of{array_value} {}
};
struct foo {
foo() {};
};
struct bar : foo {
using foo::foo;
copy_of<void> a;
};
int main() {
bar etc;
}
-----
$ g++ -std=c++23 a.cc
/usr/bin/ld: /tmp/ccqAtwru.o: in function `bar::bar()':
a.cc:(.text._ZN3barC2Ev[_ZN3barC5Ev]+0x18): undefined reference to
`copy_of<void>::copy_of()'
collect2: error: ld returned 1 exit status
-----
Fails with both gcc 15 & 16.
clang 20, 21, 22 compile it successfully.
msvc 19.50 throws ICE.
If we remove the unused template on 'copy_of' we get a better diagnostic:
a.cc: In constructor ‘constexpr copy_of::copy_of()’:
a.cc:13:45: error: call to consteval function
‘((copy_of*)this)->copy_of::array_of.array_of::array_of(array_value)’ is not a
constant expression
13 | constexpr copy_of() : array_of{array_value} {}
| ^
a.cc:13:45: in ‘constexpr’ expansion of
‘((copy_of*)this)->copy_of::array_of.array_of::array_of(array_value)’
a.cc:6:8: error: modification of ‘*(copy_of*)this’ from outside current
evaluation is not a constant expression
6 | struct array_of {
| ^~~~~~~~
a.cc:6:8: note: ‘consteval array_of::array_of(const array_of&)’ was promoted to
an immediate function because its body contains an immediate-escalating
expression ‘consteval_copy((*<anonymous>))’
-----
I would expect that the immediate invocation of 'copy_of::copy_of()' could be
inlined into bar's constructor. Indeed, if we remove the C-style array it works
fine. 'std::tuple<consteval_copy, consteval_copy>' also works. I would imagine
making a 'template <class T, std::size_t N> array_tuple' class where each
"element" is stored as a member would also work.