C++ 为什么从void pointer*强制转换的数组不能迭代for循环?

C++ 为什么从void pointer*强制转换的数组不能迭代for循环?,c++,C++,我创建了一个包含5个整数元素的数组,并将其传递给test方法,该方法使用void指针获取其参数 将其转换为整数数组后,我尝试使用for语句循环数组 但它不起作用。为什么会这样 #include <iostream> using namespace std; void test(void* arr){ int* arr2 = static_cast<int*>(arr); for(int num:arr2){ cout <<

我创建了一个包含5个整数元素的数组,并将其传递给
test
方法,该方法使用void指针获取其参数

将其转换为整数数组后,我尝试使用
for
语句循环数组

但它不起作用。为什么会这样

#include <iostream>

using namespace std;

void test(void* arr){
    int* arr2 = static_cast<int*>(arr);
    for(int num:arr2){
        cout << num << endl;
    }
}

int main(){
    int arr[5] = {1,2,3,4,5};
    test(arr);
}
#包括
使用名称空间std;
无效测试(无效*arr){
int*arr2=静态_转换(arr);
for(int num:arr2){

cout
arr
arr2
是指针,而不是数组。它们指向的数组长度对
test
来说是未知的,因此基于范围的
不会在数组上迭代。

arr
arr2
是指针,而不是数组。它们指向的数组长度对
test>来说是未知的,因此基于范围的
for
不会在数组上迭代。

您不能使用基于范围的
for
在未知边界的数组上迭代或“在”指针上迭代。编译器不知道在何处停止

假设由于某种原因,
arr
参数固定为
void*
,则可以显式传递大小:

void test(void* arr, std::size_t size) {
    int* arr2 = static_cast<int*>(arr);
    while (size-- > 0)
        std::cout << *arr2++ << std::endl;
}

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    test(arr, 5);
}
在此之前,您可以使用自制的变通方法:

template<typename T>
class my_span {
public:
    my_span(T* first, std::size_t size) :
        first_(first), size_(size) {}

    T* begin() const {
        return first_;
    }

    T* end() const {
        return first_ + size_;
    }

private:
    T* const first_;
    const std::size_t size_;
};

void test(void* arr, std::size_t size) {
    int* arr2 = static_cast<int*>(arr);
    my_span span(arr2, size);
    for (int num : span)
        std::cout << num << std::endl;
}
模板
我的班{
公众:
my_span(T*first,std::size_T size):
第一个(第一个),大小(大小){}
T*begin()常量{
先返回;
}
T*end()常数{
返回第一个字符+大小字符;
}
私人:
T*常数优先;
const std::size\u t size\u;
};
空隙试验(空隙*arr,标准::尺寸{
int*arr2=静态_转换(arr);
我的_跨度(arr2,尺寸);
for(int num:span)

std::cout您不能使用基于范围的
for
来迭代未知绑定数组或“覆盖”指针。编译器不知道在哪里停止

假设由于某种原因,
arr
参数固定为
void*
,则可以显式传递大小:

void test(void* arr, std::size_t size) {
    int* arr2 = static_cast<int*>(arr);
    while (size-- > 0)
        std::cout << *arr2++ << std::endl;
}

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    test(arr, 5);
}
在此之前,您可以使用自制的变通方法:

template<typename T>
class my_span {
public:
    my_span(T* first, std::size_t size) :
        first_(first), size_(size) {}

    T* begin() const {
        return first_;
    }

    T* end() const {
        return first_ + size_;
    }

private:
    T* const first_;
    const std::size_t size_;
};

void test(void* arr, std::size_t size) {
    int* arr2 = static_cast<int*>(arr);
    my_span span(arr2, size);
    for (int num : span)
        std::cout << num << std::endl;
}
模板
我的班{
公众:
my_span(T*first,std::size_T size):
第一个(第一个),大小(大小){}
T*begin()常量{
先返回;
}
T*end()常数{
返回第一个字符+大小字符;
}
私人:
T*常数优先;
const std::size\u t size\u;
};
空隙试验(空隙*arr,标准::尺寸{
int*arr2=静态_转换(arr);
我的_跨度(arr2,尺寸);
for(int num:span)

std::cout数组衰减为指针并丢失函数中的所有大小信息。下面是一个带模板的解决方案:

#include <iostream>

template <typename T>
void test(T const& arr) {
  for (auto const& num : arr) {
    std::cout << num << '\n';
  }
}

int main() {
  int arr[5] = {1, 2, 3, 4, 5};
  test(arr);
}
#包括
样板
无效试验(T常数和arr){
用于(自动常量和数量:arr){

std::cout数组衰减为指针并丢失函数中的所有大小信息。下面是一个带模板的解决方案:

#include <iostream>

template <typename T>
void test(T const& arr) {
  for (auto const& num : arr) {
    std::cout << num << '\n';
  }
}

int main() {
  int arr[5] = {1, 2, 3, 4, 5};
  test(arr);
}
#包括
样板
无效试验(T常数和arr){
用于(自动常量和数量:arr){


std::cout如果编译器不知道元素大小,它应该如何跳转到下一个元素?您的代码中有
(int-num:arr)
,而不是
(int-num:arr2)
@Evg抱歉我编辑了它!动态数组也是如此吗?编译器应该如何知道在哪里停止?对于C++@Kiruahxh或
std:array中的常见操作,请使用std::vector而不是type*。如果在编译时知道大小,如果不知道元素大小,编译器应该如何跳转到下一个元素?您有
(int-num:arr)
在您的代码中,而不是
(int-num:arr2)
@Evg抱歉我编辑了它!动态数组也是如此吗?编译器应该如何知道在哪里停止?对于C++@Kiruahxh中的常见操作,请使用std::vector而不是type*,或者如果在编译时知道大小(int-num:arr),请使用
std:array
{我不能确信这两种方法都有效。无论是在
test
中使用
arr
还是
arr2
,代码都不会编译,因为这两种方法都是指针。@jkb我认为它们主要是指
arr
comment@M.M很抱歉,arr不只是一个地址吗?所以我指的是指针,因为我们可以使用它们取消引用。@jwkoo混淆之处在于有两个不同的变量叫做
arr
(main中的数组和
test
中的指针)。如果
x
是数组,则可以使用
int num:x
语法,但如果
x
是指针,则不能使用
x
语法。for(int num:arr){我不能确信这两种方法都有效。无论是在
test
中使用
arr
还是
arr2
,代码都不会编译,因为这两种方法都是指针。@jkb我认为它们主要是指
arr
comment@M.M很抱歉,arr不只是一个地址吗?所以我指的是指针,因为我们可以使用它们取消引用。@jwkoo混淆之处在于,有两个不同的变量称为
arr
(main中的数组和
test中的指针)。如果
x
是数组,则可以使用
int num:x
语法,但如果
x
是指针,则不能使用语法。谢谢您的帮助。那么我应该使用哪种语法?@jwkoo,您想要基于范围的for循环?谢谢您的帮助。那么我应该使用哪种语法?@jwkoo,您想要基于范围的for循环?