C ++模板参数扣除的std ::阵列与非为size_t整数 [英] C++ template parameter deduction for std::array with non size_t integer

查看:170
本文介绍了C ++模板参数扣除的std ::阵列与非为size_t整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图适应的解决方案presented在<一个href=\"http://stackoverflow.com/questions/21723799/avoiding-struct-in-variadic-template-function\">Avoiding在结构可变参数模板函数,以我的需要。但是,我无法理解G ++的行为。考虑下面的函数:

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:

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

然后调用

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

不匹配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>’

然而,它匹配,如果我修改无符号的大小无符号长大小为size_t 。我不知道,了解这里发生了什么。是不是尺寸参数调用的std ::阵列&LT; T,尺寸和GT; 转换为为size_t

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 ?

推荐答案

的std :: 是模板为数组

std::array is templated as:

template<class T, std::size_t N > struct array;

而大小 N 要求是类型为size_t 。但是,在你的函数,你传递一个无符号(INT),它不能作为PTED 为size_t 间$ P $。据 SFINAE 如果不能扣除的模板,它不存在,因此你的模板功能不存在的。

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 <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.

由DYP附加说明:

<强> [temp.deduct.type] / 17用于非类型模板参数需要的推导的东西(模板参数)的类型是相同的类型为模板参数的它推导的。

[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.

这篇关于C ++模板参数扣除的std ::阵列与非为size_t整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆