如何通过引用将数组传递给函数模板

时间:2022-05-07 22:55:54

I am learning c++ template concepts. I do not understand the following.

我正在学习c++模板概念。我不明白下面这些。

#include <iostream>
#include <typeinfo>

using namespace std;

template <typename T>
T fun(T& x)
{
 cout <<" X is "<<x;
 cout <<"Type id is "<<typeid(x).name()<<endl;
}


int main ( int argc, char ** argv)
{
   int a[100];
   fun (a);
}

What i am trying?

我在什么?

1) T fun (T & x)

1) T fun (T & x)

Here x is a reference, and hence will not decayed 'a' into pointer type, but while compiling , i am getting the following error.

这里x是一个引用,因此不会将“a”衰减为指针类型,但是在编译时,我将得到以下错误。

 error: no matching function for call to ‘fun(int [100])’

When I try non-reference, it works fine. As I understand it the array is decayed into pointer type.

当我尝试不引用时,效果很好。据我所知,该数组已衰变为指针类型。

2 个解决方案

#1


18  

C-style arrays are very basic constructs which are not assignable, copyable or referenceable in the way built-ins or user defined types are. To achieve the equivalent of passing an array by reference, you need the following syntax:

c风格的数组是非常基本的结构,它们不能在构建ins或用户定义类型的方式中可分配、可复制或可引用。要实现按引用传递数组的等效功能,需要以下语法:

// non-const version
template <typename T, size_t N>
void fun( T (&x)[N] ) { ... }

// const version
template <typename T, size_t N>
void fun( const T (&x)[N] ) { ... }

Note that here the size of the array is also a template parameter to allow the function to work will all array sizes, since T[M] and T[N] are not the same type for different M, N. Also note that the function returns void. There is no way of returning an array by value, since the array is not copyable, as already mentioned.

注意,数组的大小也是允许函数工作的模板参数,所有的数组大小都是相同的,因为T[M]和T[N]对于不同的M不是相同的类型。无法按值返回数组,因为数组不可复制,如前所述。

#2


4  

The problem is in the return type: you cannot return an array because arrays are non-copiable. And by the way, you are returning nothing!

问题在于返回类型:您不能返回数组,因为数组是不可复制的。顺便说一句,你什么也没回来!

Try instead:

试一试:

template <typename T>
void fun(T& x)  // <--- note the void
{
    cout <<" X is "<<x;
    cout <<"Type id is "<<typeid(x).name()<<endl;
}

And it will work as expected.

它会像预期的那样工作。

NOTE: the original full error message (with gcc 4.8) is actually:

注意:原始的完整错误消息(包含gcc 4.8)实际上是:

test.cpp: In function ‘int main(int, char**)’:
test.cpp:17:10: error: no matching function for call to ‘fun(int [100])’
    fun (a);
      ^
test.cpp:17:10: note: candidate is:
test.cpp:7:3: note: template<class T> T fun(T&)
 T fun(T& x)
   ^
test.cpp:7:3: note:   template argument deduction/substitution failed:
test.cpp: In substitution of ‘template<class T> T fun(T&) [with T = int [100]]’:
test.cpp:17:10:   required from here
test.cpp:7:3: error: function returning an array

The most relevant line is the last one.

最相关的一行是最后一行。

#1


18  

C-style arrays are very basic constructs which are not assignable, copyable or referenceable in the way built-ins or user defined types are. To achieve the equivalent of passing an array by reference, you need the following syntax:

c风格的数组是非常基本的结构,它们不能在构建ins或用户定义类型的方式中可分配、可复制或可引用。要实现按引用传递数组的等效功能,需要以下语法:

// non-const version
template <typename T, size_t N>
void fun( T (&x)[N] ) { ... }

// const version
template <typename T, size_t N>
void fun( const T (&x)[N] ) { ... }

Note that here the size of the array is also a template parameter to allow the function to work will all array sizes, since T[M] and T[N] are not the same type for different M, N. Also note that the function returns void. There is no way of returning an array by value, since the array is not copyable, as already mentioned.

注意,数组的大小也是允许函数工作的模板参数,所有的数组大小都是相同的,因为T[M]和T[N]对于不同的M不是相同的类型。无法按值返回数组,因为数组不可复制,如前所述。

#2


4  

The problem is in the return type: you cannot return an array because arrays are non-copiable. And by the way, you are returning nothing!

问题在于返回类型:您不能返回数组,因为数组是不可复制的。顺便说一句,你什么也没回来!

Try instead:

试一试:

template <typename T>
void fun(T& x)  // <--- note the void
{
    cout <<" X is "<<x;
    cout <<"Type id is "<<typeid(x).name()<<endl;
}

And it will work as expected.

它会像预期的那样工作。

NOTE: the original full error message (with gcc 4.8) is actually:

注意:原始的完整错误消息(包含gcc 4.8)实际上是:

test.cpp: In function ‘int main(int, char**)’:
test.cpp:17:10: error: no matching function for call to ‘fun(int [100])’
    fun (a);
      ^
test.cpp:17:10: note: candidate is:
test.cpp:7:3: note: template<class T> T fun(T&)
 T fun(T& x)
   ^
test.cpp:7:3: note:   template argument deduction/substitution failed:
test.cpp: In substitution of ‘template<class T> T fun(T&) [with T = int [100]]’:
test.cpp:17:10:   required from here
test.cpp:7:3: error: function returning an array

The most relevant line is the last one.

最相关的一行是最后一行。