为什么模板函数中数组会衰减为指针
我不明白为什么数组会衰减为模板函数中的指针。
如果您查看以下代码:当参数被强制为引用(函数 f1)时,它不会衰减。在另一个函数 f 中它会衰减。为什么函数 f 中 T 的类型不是 const char (buff&)[3] 而是 const char* (如果我理解正确的话)?
#include <iostream>
template <class T>
void f(T buff) {
std::cout << "f:buff size:" << sizeof(buff) << std::endl; //prints 4
}
template <class T>
void f1(T& buff) {
std::cout << "f:buff size:" << sizeof(buff) << std::endl; //prints 3
}
int main(int argc, char *argv[]) {
const char buff[3] = {0,0,0};
std::cout << "buff size:" << sizeof(buff) << std::endl; //prints 3
f(buff);
f1(buff);
return 0;
}
I don't understand why the array decays to a pointer in a template function.
If you look at the following code: When the parameter is forced to be a reference (function f1) it does not decay. In the other function f it decays. Why is the type of T in function f not const char (buff&)[3] but rather const char* (if I understand it correctly)?
#include <iostream>
template <class T>
void f(T buff) {
std::cout << "f:buff size:" << sizeof(buff) << std::endl; //prints 4
}
template <class T>
void f1(T& buff) {
std::cout << "f:buff size:" << sizeof(buff) << std::endl; //prints 3
}
int main(int argc, char *argv[]) {
const char buff[3] = {0,0,0};
std::cout << "buff size:" << sizeof(buff) << std::endl; //prints 3
f(buff);
f1(buff);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是因为数组无法按值传递给函数。因此,为了使其正常工作,数组会衰减为一个指针,然后按值传递给函数。
换句话说,按值传递数组类似于用另一个数组初始化一个数组,但在 C++ 中一个数组不能用另一个数组初始化数组:
因此,如果数组出现在
=
的右侧,则左侧必须是pointer
或reference
类型:演示:<一href="http://www.ideone.com/BlfSv" rel="noreferrer">http://www.ideone.com/BlfSv
正是出于同样的原因
auto
在下面的每种情况下都有不同的推断(请注意,auto
随 C++11 一起提供):输出:
演示:http://www.ideone.com/aXcF5
It is because arrays cannot be passed by value to a function. So in order to make it work, the array decays into a pointer which then gets passed to the function by value.
In other words, passing an array by value is akin to initializing an array with another array, but in C++ an array cannot be initialized with another array:
So if an array appears on the right hand side of
=
, the left hand side has to be eitherpointer
orreference
type:Demo : http://www.ideone.com/BlfSv
It is exactly for the same reason
auto
is inferred differently in each case below (note thatauto
comes with C++11):Output:
Demo : http://www.ideone.com/aXcF5
因为数组不能作为函数参数按值传递。
当您按值传递它们时,它们会退化为指针。
在此函数中:
T 不能是 char (&buff)[3],因为这是一个引用。编译器会尝试
char (buff)[3]
按值传递,但这是不允许的。因此,为了使其工作,数组会衰减为指针。您的第二个函数有效,因为这里数组是通过引用传递的:
Because arrays can not be passed by value as a function parameter.
When you pass them by value they decay into a pointer.
In this function:
T can not be
char (&buff)[3]
as this is a reference. The compiler would have triedchar (buff)[3]
to pass by value but that is not allowed. So to make it work arrays decay to pointers.Your second function works because here the array is passed by reference:
引用规范,它说
所以,在你的情况下,很明显,
不会衰减为指针。
To quote from spec, it says
So, in your case, It is clear that,
doesn't decay into pointer.
因为函数不能将数组作为参数。但它们可以有数组引用。
Because functions can't have arrays as arguments. They can have array references though.
原因基本上可以归结为匹配不同重载时的类型推导。当您调用
f
时,编译器会将类型推导为const char[3]
,然后衰减为const char*
因为这就是数组可以。。其完成方式与在f(1)
中编译器将 T 推导为int
而不是int&
的方式完全相同。在
f1
的情况下,因为参数是通过引用获取的,所以编译器再次将 T 推导为const char[3]
,但它需要对其的引用。没有什么真正令人惊讶的,但如果不是数组用作函数参数时衰减,那么就会保持一致......
The reason basically boils down to type deduction when matching the different overloads. When you call
f
the compiler deduces the type to beconst char[3]
which then decays intoconst char*
because that's what arrays do. This is done in the same exact way that inf(1)
the compiler deduces T to beint
and notint&
.In the case of
f1
because the argument is taken by reference, then the compiler again deduces T to beconst char[3]
, but it takes a reference to it.Nothing really surprising, but rather consistent if it were not for the decay of arrays to pointers when used as function arguments...