2

This is a follow-up to How to initialize std::array member in constructor initialization list when the size of the array is a template parameter. I got a great answer, but still I have an issue with g++/clang++ for my use case (program builds fine with msvc).

It looks like using the initializer list + index_sequence to initialize a std::array containing a class Hash with a deleted copy constructor fails, even if the initializer list does not contain the Hash class itself, but parameters to construct it. Unfortunately in my actual use case the copy constructor is not available because the class contains a mutex.

When building the following program, I have this error:

$ g++ -std=c++14 -c index_sequence.cxx
index_sequence.cxx: In instantiation of ‘A<H, N>::A(const std::vector<int>&, std::index_sequence<i ...>) [with long unsigned int ...i = {0ul, 1ul, 2ul, 3ul}; H = Hash; long unsigned int N = 4ul; std::index_sequence<i ...> = std::integer_sequence<long unsigned int, 0ul, 1ul, 2ul, 3ul>]’:
index_sequence.cxx:24:71:   required from ‘A<H, N>::A(const std::vector<int>&) [with H = Hash; long unsigned int N = 4ul]’
index_sequence.cxx:37:22:   required from here
index_sequence.cxx:28:86: error: use of deleted function ‘Hash::Hash(const Hash&)’
     A(const vector<int> &data, std::index_sequence<i...>) : hashes{((void)i, data)...} {}

and here is the program:

#include <vector>
#include <array>
#include <utility>

using namespace std;

struct Hash {
    const vector<int> &data;

    Hash(const vector<int> &data)
        : data(data) {
    }

    Hash(const Hash &) = delete;

    uint64_t operator()(int id) const {
        return data[id];
    }
};

template <class H, size_t N>
class A {
public:
    A(const vector<int> &data) : A(data, std::make_index_sequence<N>{})  {
    }

    template <std::size_t... i>
    A(const vector<int> &data, std::index_sequence<i...>) : hashes{((void)i, data)...} {}

    std::array<H, N> hashes;
};
    

int main () {
    vector<int> data{1, 2, 3, 4};

    A<Hash, 4> a{data};
}

Since the same program builds fine with msvc, I wonder if this is an issue with g++'s std::array initializer_list support?

greg_p
  • 315
  • 1
  • 8

0 Answers0