I have some code that compiles and runs fine under g++ 10.2.0, but is rejected by clang++ 11.0.0.
Here is a minimal reproducer of the issue:
#include <tuple>
#include <cstdint>
struct Dummy { };
using second_t = Dummy;
using example_t = std::tuple<size_t, second_t[8]>;
example_t f() {
example_t result;
return result;
}
int main() {
auto x = f();
(void) x;
}
In g++, it compiles with no complaints, but using clang++ I get:
clang++ --std=c++20 -Wall -Werror main.cpp -o example
In file included from main.cpp:1:
/usr/bin/../lib/gcc/aarch64-unknown-linux-gnu/10.2.0/../../../../include/c++/10.2.0/tuple:137:4: error: array initializer must be an initializer list
: _M_head_impl(std::forward<_UHead>(__h)) { }
^
/usr/bin/../lib/gcc/aarch64-unknown-linux-gnu/10.2.0/../../../../include/c++/10.2.0/tuple:375:9: note: in instantiation of function template specialization 'std::_Head_base<1, Dummy [8], false>::_Head_base<Dummy [8]>' requested here
: _Base(std::forward<_Head>(_M_head(__in))) { }
^
/usr/bin/../lib/gcc/aarch64-unknown-linux-gnu/10.2.0/../../../../include/c++/10.2.0/tuple:236:9: note: in instantiation of member function 'std::_Tuple_impl<1, Dummy [8]>::_Tuple_impl' requested here
: _Inherited(std::move(_M_tail(__in))),
^
/usr/bin/../lib/gcc/aarch64-unknown-linux-gnu/10.2.0/../../../../include/c++/10.2.0/tuple:996:17: note: in instantiation of member function 'std::_Tuple_impl<0, unsigned long, Dummy [8]>::_Tuple_impl' requested here
constexpr tuple(tuple&&) = default;
^
main.cpp:11:12: note: in defaulted move constructor for 'std::tuple<unsigned long, Dummy [8]>' first required here
return result;
^
1 error generated.
make: *** [Makefile:2: all] Error 1
Interestingly, if I change second_t to be an int32_t, I get an error from g++ too:
g++ --std=c++20 -Wall -Werror main.cpp -o example
In file included from main.cpp:1:
/usr/include/c++/10.2.0/tuple: In instantiation of ‘constexpr std::_Head_base<_Idx, _Head, false>::_Head_base(_UHead&&) [with _UHead = int [8]; long unsigned int _Idx = 1; _Head = int [8]]’:
/usr/include/c++/10.2.0/tuple:375:49: required from ‘constexpr std::_Tuple_impl<_Idx, _Head>::_Tuple_impl(std::_Tuple_impl<_Idx, _Head>&&) [with long unsigned int _Idx = 1; _Head = int [8]]’
/usr/include/c++/10.2.0/tuple:237:42: required from ‘constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(std::_Tuple_impl<_Idx, _Head, _Tail ...>&&) [with long unsigned int _Idx = 0; _Head = long unsigned int; _Tail = {int [8]}]’
/usr/include/c++/10.2.0/tuple:996:17: required from here
/usr/include/c++/10.2.0/tuple:137:42: error: array used as initializer
137 | : _M_head_impl(std::forward<_UHead>(__h)) { }
| ^
make: *** [Makefile:2: all] Error 1
I'm guessing it's down to the use of a C-array. If I change the code to use std::array instead, the problem goes away. However, I'm still curious about whether there is a way to do this with C-array for the sake of curiosity.
So my questions are:
- Was g++ wrong to accept this code?
- Is there any way to return a C-array inside of a tuple like this that works for g++ and clang++?
std::arrayinstead of a plain array. You won't notice a different and there will be fewer unexpected surprises to deal with, and fewer headaches.std::arrayis literallytemplate <typename T, std::size_t N> struct array { functions; private: T data[N]; };. If an array is safe for you, so isstd::arraystd::is_move_constructible<Ti>::value must be true for all i, otherwise the behavior is undefined. For C-arrays this is wrong, so we get undefined behaviorstd::array, you can always usestruct scared_of_the_STL { second_t data[8]; };. This is common in C to have easy-to-copy arrays.