0

I am using test code with some test data as below. I would really like to eliminate count from the array declaration. Is it possible? Also why does double braces = {{ ... }} are required?

struct test_data {
    std::vector<int> nums;
    int turnToStop;
    int expectedResult;
};


int main()
{
    std::array<test_data, 5> tests = {{
        {{2, 1, 3}, 2020, 10},
        {{1, 2, 3}, 2020, 27},
        {{2, 3, 1}, 2020, 78},
        {{3, 2, 1}, 2020, 438},
        {{3, 1, 2}, 2020, 1836}
    }};
}
mike
  • 1,670
  • 11
  • 21
  • It is not possible to eliminate the count from `std::array` like this, but you could use a built-in array type: `test_data tests[] = { ... }`. You can still use for loops to iterate over such an array, and `std::begin()` and `std::end()` still work correctly (they have specializations for arrays). – Dietrich Epp Jan 04 '21 at 00:48
  • The question does not have an answer, so it looks like there is no way with `std::array` without copying (And would also suggest that a plain C array like above, `test_data tests[] = { ... }`, is good enough) – Artyer Jan 04 '21 at 00:53

1 Answers1

0

I would really like to eliminate count from the array declaration. Is it possible?

With standard library, no.

There is a make_array that is supposed to eliminate the count when initialising std::array, but it's not standardised yet:

Current status of std::make_array

Also why does double braces = {{ ... }} are required?

{{2, 1, 3}, 2020, 10},

Double braces are required because the inner pair of braces are for initialising your vector nums

artm
  • 17,291
  • 6
  • 38
  • 54
  • `std::make_array` would require `auto tests = make_array(test_data{{2, 1, 3}, 2020, 10}, test_data{{1, 2, 3}, 2020, 27}, ...)`, repeating the type name over and over – Artyer Jan 04 '21 at 00:54
  • The op is referring to the outermost double braces – bolov Jan 04 '21 at 01:03
  • @Artyer no you can write `make_array({...}, {...})` ... I think ... can't look into it right now – bolov Jan 04 '21 at 01:06
  • @bolov You can't since make_array takes `T&&...`, so you can't use an braced initializer lists to deduce a `T`. Also https://en.cppreference.com/w/cpp/experimental/make_array claims that it has been removed because of deduction guides and `std::to_array`, and I think `std::to_array({{...}, {...}})` perfectly answers this question – Artyer Jan 04 '21 at 01:54
  • The first template parameters is a special one that if provided and different than `void` is used to explicitly specify the type of the element. So it seems you can and that is by design. The `to_array` seems to have the problem of copying the elements. Again I am going only from a rudimentary reading of the documentation. Can't properly check this assertions. – bolov Jan 04 '21 at 01:58
  • 1
    @Artyer `to_array` works very nice: https://coliru.stacked-crooked.com/a/a5aeba9f02a3bf80. Actually that would be a perfect answer to my question, but I see question was closed. – mike Jan 04 '21 at 10:16
  • ....but in the linked answer there seems to be no mention of std::to_array, I think it should be added there. – mike Jan 04 '21 at 10:18