How to get the compiler to output the correct integer pattern for me

Consider this part:

template <int N> void fill_with_magic(array<int, N>& whatever){
    for(int i = 0; i < N; i++){
        whatever[i] = magic(i, N);
    }
}

I call it on a specific instance, so for an array of 3 I would have to do:

array<int, 3> some_array_of_3;
fill_with_magic<3>(some_array_of_3);

But do I really need to write <3>? The compiler already knows the size of the array, so theoretically it could infer the correct instance based on this size. Can I do it?

+4
source share
1 answer

The problem is the output of the argument: the second argument of the template is std::arraynot int, therefore, the output is not executed because it requires conversion.

You must define your method as

template <array<int, 0>::size_type N> void fill_with_magic(array<int, N>& whatever){
  for(int i = 0; i < N; i++){
    whatever[i] = magic(i, N);
  }
}

, array<int,0>::size_type , . , , size_t, ( std::array<T, 0> ).

+6

Source: https://habr.com/ru/post/1608146/


All Articles