My class has a constexpr constructor which takes a reference to a std::array
. When creating a global constexpr constant of my class and passing an immediate array, the constructor gets a null data pointer from the array reference even though the underlying data exists:
class MyClass {
public:
explicit constexpr MyClass(const std::array<int, 3> &from) {
assert(from[0] == 1); // Ok
assert(from[1] == 2); // Ok
assert(from[2] == 3); // Ok
assert(from.data() != nullptr); // Compilation halts here
}
};
static constexpr MyClass constGlobal{std::array{1, 2, 3}};
Requires C++ 17+
Tested on:
MinGW w64 9.0
ARM gcc 12.2
I would expect the data()
pointer to exist considering operator[]
works perfectly fine.
EDIT:
The data()
pointer does exist. This seems to be an issue with GCC not allowing pointer comparison to temporaries. There still exists a related problem which this simplified example attempted to describe (badly).
EDIT:
Looks like this is the likely gcc bug for comparison of addresses of temporaries in constexpr context: