[英]What happens when a short int[] is automatically cast to long int[]?
在C ++中,如果我有一个短整数数组并将其作为参数传递给采用长整数数组的函数,则该函数会看到短数组的每个值都转换为长整数,还是会重新解释整个数组到n / 2个元素的长整数数组中?
You can't because there's no automatic cast that can be used. 您不能,因为没有可以使用的自动强制转换。
Remember that you can't pass an array to a function by value: an array parameter is really just a pointer parameter, so the following two are the same: 请记住,您不能按值将数组传递给函数:数组参数实际上只是一个指针参数,因此以下两个相同:
void f(long a[]);
void f(long* a);
When you pass an array to a function, the array is implicitly converted to a pointer to its initial element. 将数组传递给函数时,该数组将隐式转换为指向其初始元素的指针。 So, given
long data[10];
因此,给定
long data[10];
, the following two are the same: ,以下两个相同:
f(data);
f(&data[0]);
There is no implicit conversion from short*
to long*
, so if data
were declared as short data[10];
没有从
short*
到long*
隐式转换,因此如果将data
声明为short data[10];
, you would get a compilation error. ,则会出现编译错误。
You would need to explicitly cast the short*
to a long*
, using reinterpret_cast
, but that won't convert "an array of N short
elements" into "an array of N long
elements," it will reinterpret the pointed-to data as "an array of [some number of] long
elements," which probably isn't what you want. 您将需要使用
reinterpret_cast
将short*
明确地转换为long*
,但这不会将“ N个short
元素的数组”转换为“ N个long
元素的数组”,它将指向的数据重新解释为“ [一些] long
元素的数组”,这可能不是您想要的。
You need to create a new array of long
elements and copy the elements from the array of short
into the array of long
: 您需要创建一个新的
long
元素数组,并将元素从short
数组复制到long
数组:
short data[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
long data_as_long[10];
std::copy(data, data + 10, data_as_long);
f(data_as_long);
Or, you might consider changing your function to be a template that can accept a pointer to an array of any type of element: 或者,您可以考虑将函数更改为可以接受指向任何类型的元素数组的指针的模板:
template <typename T> void f(T*);
Or, for a more advanced solution, the most generic way to do this would be to have your function take an iterator range. 或者,对于更高级的解决方案,最通用的方法是使函数采用迭代器范围。 This way, you could pass it any type of iterator (including pointers into an array).
这样,您可以将任何类型的迭代器(包括指向数组的指针)传递给它。 In addition, it provides a natural and simple way to ensure that the length of the array is passed correctly, since the length is not passed explicitly, it's determined by the arguments.
此外,它提供了一种自然而简单的方法来确保正确传递数组的长度,因为未显式传递长度,所以长度由参数决定。
template <typename Iterator> void f(Iterator first, Iterator last);
const unsigned length_of_data = 10;
long data_array[length_of_data];
std::vector<long> data_vector(length_of_data);
f(data_array, data_array + length_of_data); // works!
f(data_vector.begin(), data_vector.end()); // also works!
No, a good compiler will give an error. 不,好的编译器会报错。 A bad compiler will give strange results.
错误的编译器会给出奇怪的结果。
Recasting a single element at a time is common and presents no difficulties, but passing a pointer to a function expecting one type of object and receiving another is an I Love Lucy sort of catastrophe. 一次重铸一个元素很普遍,没有任何困难,但是将指针传递给期望一种类型的对象并接收另一种类型的函数,这就是我爱露西的一种灾难。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.