向指针数组添加左const [英] Adding a left const to an array of pointers

查看:90
本文介绍了向指针数组添加左const的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一次处理多个数组。有些函数需要更改数组
,其他函数只需要读取即可。例如,这些函数:

I work with multiple arrays at a time. There are functions which need to change the arrays, others only need to read. Take for instance these functions:

// Only needs to read from the two arrays.
void f(int const *a[2]) { /* ... */ }

// Changes the two arrays.
void g(int *a[2]) { /* ... */ }



<我希望我可以用非常量数组来调用它们。总是可以在最右边添加
const ,在左边添加 const 可以更改
类型。我仍然认为 const 只会使它更严格,而
应该是可能的。

I would expect that I could call them both with a non-const array. Adding a right-most const is always possible, a const on the left does change the type. Still I would think that a const there would only make it stricter and should be possible.

数据结构可能看起来像这样:

A data structure might look like this:

int *a[2] = {new int[10], new int[10]};

调用 g 没问题,干净地编译:

Calling g is not a problem, that compiles cleanly:

g(a);

但是,当我尝试使用 f ,则不起作用:

However, when I try the same with f, it does not work:

f(a);

g ++

const_cast.cpp: In function 'int main(int, char**)':
const_cast.cpp:12:8: error: invalid conversion from 'int**' to 'const int**' [-fpermissive]
     f(a);
        ^

clang ++ 给出的信息略有不同:

The message that clang++ gives is slightly different:

const_cast.cpp:12:5: error: no matching function for call to 'f'
    f(a);
    ^
const_cast.cpp:4:6: note: candidate function not viable: no known conversion from 'int *[2]' to 'const int **' for 1st
      argument
void f(int const *a[2]) {}
     ^

然后,我尝试使用 const_cast 强制进行此转换:

Then I have tried to use a const_cast to force this conversion:

f(const_cast<int const(*)[2]>(a));

这是行不通的,因为它也会更改间接寻址的数量。

That does not work, because it changes the number of indirections as well.

const_cast.cpp:22:36: error: invalid const_cast from type 'int**' to type 'const int (*)[2]'
     f(const_cast<int const(*)[2]>(a));
                                    ^

我不想放宽对 f的要求。对
用户应该清楚此函数不会更改数组的内容。另外
在代码库中还有其他我不想更改的情况。

I do not want to relax the requirement on f, though. It should be clear to the user that this function will not alter the contents of the array. Also there are other cases in the codebase that I do not want to change.

使用正确的 const <创建新变量/ code>确实有效,但是:

Creating a new variable with the right const does work, however:

int const *const_a[2] = {a[0], a[1]};
f(const_a);

是否有更简单的方法?

推荐答案

int const * int * 是不相关的类型,并且您不能将指向后者的指针转换为指向前者的指针。

int const* and int* are unrelated types, and you can't convert a pointer to the latter into a pointer to the former.

您无法执行此操作,因为它并不安全,但问题是不是该函数可能会修改该数组,而是可以用不可修改的数组替换该数组。

You can't do this because it wouldn't be safe, but the problem isn't that the function might modify the array, it's that it could replace the array with one that isn't modifiable.

考虑

int const constants[] = {1,2,3};

void f(int const** a)
{
    *a = constants; // OK, *a and &constants[0] are int const*.
}

int main() 
{
    int* x;
    f(&x);
    x[0] = 0; // Modifying a const object; undefined behaviour.
}

(请注意,在函数参数中, T x [ 2] 等效于 T x [] T * x 。)

(Note that in function parameters, T x[2] is equivalent to T x[]and T* x.)

这篇关于向指针数组添加左const的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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