https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118264
Bug ID: 118264
Summary: GCC allows declaring constexpr template constructor in
derived type with virtual base
Product: gcc
Version: 15.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: wangbopku15 at gmail dot com
Target Milestone: ---
The following code does not trigger any error in GCC:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
struct single
{
};
struct derived : virtual single
{
template <typename Arg>
constexpr derived(Arg arg){}
};
auto obj = derived{1};
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Note that a normal ctor without template can be correctly rejected.
MSVC, EDG, and Clang reject it by giving diagnostics like:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:9:15: error: constexpr constructor not allowed in struct with virtual
base class
9 | constexpr derived(Arg arg){}
| ^
<source>:6:18: note: virtual base class declared here
6 | struct derived : virtual single
| ^~~~~~~~~~~~~~
<source>:12:12: error: no matching constructor for initialization of 'derived'
12 | auto obj = derived{1};
| ^ ~~~
<source>:6:8: note: candidate constructor (the implicit copy constructor) not
viable: no known conversion from 'int' to 'const derived' for 1st argument
6 | struct derived : virtual single
| ^~~~~~~
<source>:6:8: note: candidate constructor (the implicit move constructor) not
viable: no known conversion from 'int' to 'derived' for 1st argument
6 | struct derived : virtual single
| ^~~~~~~
2 errors generated.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Please see https://godbolt.org/z/bYaeEYE3a