<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/63445>63445</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
member call on member 'data_' of union with no active member is not allowed in a constant expression
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
z0nt
</td>
</tr>
</table>
<pre>
Clang refuses to compile the following code:
```c++
#include <stdlib.h>
template <class T, int N>
class Array {
public:
template <typename... Args>
constexpr Array(Args... args) noexcept {
static_assert(sizeof...(args) <= N);
ArrayBuilder(args...);
}
constexpr void push_back(const T& value) noexcept {
if (size_ < N) {
data_[size_++] = value;
} else {
abort();
}
}
constexpr int size() const { return size_; }
constexpr const T& operator[](int pos) const { return data_[pos]; }
private:
constexpr void ArrayBuilder() noexcept { }
template <typename _T, typename... Args>
constexpr void ArrayBuilder(_T value, Args... args) noexcept {
data_[size_++] = value;
ArrayBuilder(args...);
}
int size_ = 0;
union {
T data_[N];
};
};
class Int {
public:
Int() = delete;
constexpr explicit Int(int i) noexcept : i_(i) {}
constexpr operator int() const { return i_; }
private:
//int i_; // works
int i_ = 0; // doesn't compile
};
static constexpr Array<Int, 1> GetInts() {
Array<Int, 1> ret;
ret.push_back(Int(0x5a));
return ret;
}
//static constexpr Array<Int, 1> GetInts() { return {Int(0x5a)}; }
//static constexpr Array<int, 1> GetInts() { return {int(0x5a)}; }
int main() {
constexpr auto test = GetInts();
static_assert(test.size() == 1);
static_assert(test[0] == 0x5a);
}
```
with the following diagnostic:
```
main.cc:59:18: error: constexpr variable 'test' must be initialized by a constant expression
constexpr auto test = GetInts();
^ ~~~~~~~~~
main.cc:14:22: note: member call on member 'data_' of union with no active member is not allowed in a constant expression
data_[size_++] = value;
^
main.cc:52:7: note: in call to '&ret->push_back(Int(90))'
ret.push_back(Int(0x5a));
^
main.cc:59:25: note: in call to 'GetInts()'
constexpr auto test = GetInts();
^
```
Compiles just fine with GCC & MSVC -- https://godbolt.org/z/5qdh7cq6a
Eventually I've made code compiling with Clang. The only change I've made is removed default initialization of member of`class Int` (which should not make any difference at all, because there is explicit constructor and default one is deleted). There is also different version of `GetInts()` which uses template parameter pack, but it fails to compile as well (though GCC & MSVC compile it as well with no problem).
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8V09z4j4S_TTi0hWXkbENBw4Bkqkcdi6b2islyw3WjCx5JJmEHPazb0m2wUCSTebwS1EEUP95_fp122bWir1CXJJ0RdLNhLWu0mb5Fis3KXR5XK4lU3swuGstWnAauK4bIRFchbDTUuoXofbAdYkkuSfxhsT3JIu7Fyd05V_drzQRisu2RCDJ2rpSiiKqSPLQH4d3h3UjmQsmXDJr4ZnQNQjl4OfJtDu4N4YdgeR9eGjaQgp-AgEwjuWODSpWYxRFcG_29hQLuFbW4WtjuoCEzv25t2Peji5AaXzl2LhRLgCwjjnBt8xaNI7QuRVvqHdRFBE6HzxJsibJBn4SuiDJyDdkWrVClmh68-A4tiL5ZkwMjIAetCihaW21LRj_Teg8HHmmMjgw2eLHqMUOeqxbjy5AG1mUzLEtSVfBoG9eugFfRBd4XAXJN4DS4mUGAFbowMh11aeKPqvNd9pn7_y7A58ADLrWKOiQJatRtJH3iAjdoGFOm07YhM595Ebbd6MOdfvzdHMRflCXEQfmzhq_bsdVS687cFvye-qEbVD7x2L9P1m3z0P_1_A1FX-r4V-Q7U2ZQz-3IWY8itcqodUlmucTnp9dG0ZRhy8Xn0fr4Em5z5bBk-olGXCUKNGNizvziq-NFFy43sPjF5f8Jfcgtv5omJ33lDjIzxPwkZbF9ktKA0IfCX0MSIJH_wO8aPPbjokWZ5ZPRqVGqwjN3bC5P6Kxe-_W2s1WTNaBjjVMSfIAP9A9KWcHQs8tfM_YoBsRbdBF49XVsRy_pszr6GJj9CyN_c9cheL-DuwQmOSry-z51eR_nkN8MYf4NEd4972rmVC3hJ7TstZpcOgVlGwuE545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