C++ template parameter deduction for std::array with non size_t integer

后端 未结 2 1759
没有蜡笔的小新
没有蜡笔的小新 2021-01-18 08:23

I\'m trying to adapt the solution presented in Avoiding struct in variadic template function to my need. However, I can\'t understand the the behavior of G++. Consider the f

2条回答
  •  长发绾君心
    2021-01-18 09:22

    std::array is templated as:

    template struct array;
    

    while the size N is required to be the type size_t. But in your function, you are passing an unsigned (int) which cannot be interpreted as size_t. According to SFINAE If a template cannot be deducted, it does not exist, thus your templated function does not exist.

    It is NOT the problem with the call line, but your declaration of your function template. To correct this, use the correct type:

    template 
    int nextline(const typename std::array ar) {
      return 0;
     }
    

    In this case, even you use:

    nextline(std::array { 1,0 });
    

    It still works because it can be deducted and casted.


    Additional explanation by dyp:

    [temp.deduct.type]/17 for non-type template parameters that requires the type of the deduced thing (template-argument) to be of the same type as the template-parameter it is deduced for.

提交回复
热议问题