clang fails to compile valid constexpr default constructor
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
The following code declares a trivial `default` default constructor when `use_trivial` is true, and implements a non-trivial default constructor that initializes the value to zero, when `use_trivial` is `false`.
This is valid according to my reading of the standard, and it compiles as expected on all versions of gcc from version 10 up to the current version. It fails to compile on all versions of clang that support C++20.
Specifically, the line defining `blarg1` gets an error like `error: constexpr variable 'blarg1' must be initialized by a constant expression`.
FWIW, when default constructing a value at runtime, it seems to do the right thing, so the constructor is there.
```c++
template inline constexpr bool use_trivial = true;
template <> inline constexpr bool use_trivial = false;
template
struct Blarg
{
T _;
constexpr Blarg() noexcept
requires(not use_trivial)
: _(0)
{ }
constexpr Blarg() noexcept
requires use_trivial
= default;
};
static_assert(std::is_default_constructible_v>);
static_assert(std::is_trivially_default_constructible_v>);
constexpr Blarg blarg0{};
static_assert(std::is_default_constructible_v>);
static_assert(not std::is_trivially_default_constructible_v>);
constexpr Blarg blarg1;
```
Contributor guide
Research direction
Start by compiling the provided C++20 reproducer with a Clang version that supports C++20 and compare the constexpr diagnostics with GCC. Focus on the constrained default constructors in Blarg and the constexpr declaration of blarg1. Done means Clang accepts the valid example without the constant-expression error while preserving the shown constructibility properties.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100