0

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:

  1. Was g++ wrong to accept this code?
  2. Is there any way to return a C-array inside of a tuple like this that works for g++ and clang++?
8
  • 1
    Whether or not it is kosher, according to the C++ standard, to have a plain array in a tuple is a fine question. However, whatever the answer is: it's just one of those things that are inadvisable, and even if it's correct I wouldn't do it. If you need an array in a tuple use std::array instead of a plain array. You won't notice a different and there will be fewer unexpected surprises to deal with, and fewer headaches. Dec 19, 2020 at 15:15
  • @SamVarshavchik whilst I agree, I can't do that, as per my comment regarding std::array in the question :)
    – OMGtechy
    Dec 19, 2020 at 15:17
  • 1
    std::array is literally template <typename T, std::size_t N> struct array { functions; private: T data[N]; };. If an array is safe for you, so is std::array Dec 19, 2020 at 15:22
  • i think it is because for copy/move constructor of tuple: std::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 behavior
    – d_kog
    Dec 19, 2020 at 15:27
  • 1
    If you don't like std::array, you can always use struct scared_of_the_STL { second_t data[8]; };. This is common in C to have easy-to-copy arrays.
    – Acorn
    Dec 19, 2020 at 15:41

1 Answer 1

-2

C arrays are basically pointers, and note that they don't provide nor need information about the length of the array. the length must be kept separately and passed around. Therefore one way to implement what you need would be:

  using example_t = std::tuple<size_t, second_t*>;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.