具有非size_t整数的std :: array的C ++模板参数推导

时间:2022-10-28 17:01:51

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 following function:

我正在尝试根据我的需要调整在可变参数模板函数中避免使用struct的解决方案。但是,我无法理解G ++的行为。考虑以下功能:

 template <typename T, unsigned Size>
 int nextline(const typename std::array<T, Size> ar) {
    return 0;
 }

Then the call

然后是电话

 nextline(std::array<int, 2> { 1,0 });

doesn't match with GCC complaining with

与GCC抱怨不符

eslong.cpp: In function ‘int main()’:
eslong.cpp:10:38: error: no matching function for call to ‘nextline(std::array<int, 2ul>)’
   nextline(std::array<int, 2> { 1,0 });
                                      ^
eslong.cpp:10:38: note: candidate is:
eslong.cpp:4:5: note: template<class T, unsigned int Size> int nextline(std::array<T, Size>)
 int nextline(const typename std::array<T, Size> ar) {
     ^
eslong.cpp:4:5: note:   template argument deduction/substitution failed:
eslong.cpp:10:38: note:   mismatched types ‘unsigned int’ and ‘#‘integer_cst’ not supported by dump_type#<type error>’
   nextline(std::array<int, 2> { 1,0 });
                                      ^
eslong.cpp:10:38: note:   ‘std::array<int, 2ul>’ is not derived from ‘std::array<T, Size>’

However it matches if I changes unsigned Size to unsigned long Size or size_t. I'm not sure to understand what's happening here. Isn't the Size parameter in the call to std::array<T, Size> converted to a size_t ?

但是,如果我将无符号大小更改为无符号长度大小或size_t,则匹配。我不太清楚这里发生了什么。是不是调用std :: array 中的Size参数转换为size_t? ,size>

2 个解决方案

#1


8  

std::array is templated as:

std :: array的模板如下:

template<class T, std::size_t N > 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.

而大小N必须是size_t类型。但是在你的函数中,你传递的是unsigned(int),它不能被解释为size_t。根据SFINAE,如果模板不能被扣除,则它不存在,因此您的模板化函数不存在。

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

这不是调用行的问题,而是您的函数模板声明。要更正此问题,请使用正确的类型:

template <typename T, size_t Size>
int nextline(const typename std::array<T, Size> ar) {
  return 0;
 }

In this case, even you use:

在这种情况下,即使您使用:

nextline(std::array<int, 2ul> { 1,0 });

It still works because it can be deducted and casted.

它仍然有效,因为它可以扣除和铸造。


Additional explanation by dyp:

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.

[temp.deduct.type] / 17表示非类型模板参数,要求推导的事物(模板参数)的类型与推导出的模板参数的类型相同。

#2


0  

Your literal 2 is interpreted as an unsigned long, but you are declaring the Size template to be an unsigned int. Just use this instead:

您的文字2被解释为unsigned long,但您声明Size模板是unsigned int。只需使用它:

template <typename T, size_t Size>

#1


8  

std::array is templated as:

std :: array的模板如下:

template<class T, std::size_t N > 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.

而大小N必须是size_t类型。但是在你的函数中,你传递的是unsigned(int),它不能被解释为size_t。根据SFINAE,如果模板不能被扣除,则它不存在,因此您的模板化函数不存在。

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

这不是调用行的问题,而是您的函数模板声明。要更正此问题,请使用正确的类型:

template <typename T, size_t Size>
int nextline(const typename std::array<T, Size> ar) {
  return 0;
 }

In this case, even you use:

在这种情况下,即使您使用:

nextline(std::array<int, 2ul> { 1,0 });

It still works because it can be deducted and casted.

它仍然有效,因为它可以扣除和铸造。


Additional explanation by dyp:

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.

[temp.deduct.type] / 17表示非类型模板参数,要求推导的事物(模板参数)的类型与推导出的模板参数的类型相同。

#2


0  

Your literal 2 is interpreted as an unsigned long, but you are declaring the Size template to be an unsigned int. Just use this instead:

您的文字2被解释为unsigned long,但您声明Size模板是unsigned int。只需使用它:

template <typename T, size_t Size>