无法从XXX **转换为const XXX ** [英] Cannot convert from XXX** to const XXX**

查看:108
本文介绍了无法从XXX **转换为const XXX **的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中有非常简单的代码.在Visual Studio下编译时,会发生错误.

I have the very simple code in c++. When I compile under Visual studio, the error happens.

#include <stdint.h>
#include <time.h>
void func1(const uint8_t* data)
{
}

void func2(const uint8_t** data)
{
}
int main() 
{
    uint8_t* data1 = NULL;
    uint8_t** data2 = NULL;
    func1(data1);//OK
    func2(data2);//error C2664: cannot convert argument 1 from 'uint8_t **' to 'const uint8_t **'
}

完整的错误信息是:

错误C2664:'void func2(const uint8_t **)':无法将参数1从'uint8_t **'转换为'const uint8_t **'

error C2664: 'void func2(const uint8_t **)' : cannot convert argument 1 from 'uint8_t **' to 'const uint8_t **'

通常您不能将const XXX转换为XXX,但是从XXX转换为const XXX应该可以,为什么在此发生此错误?

Usualy you cannot convert const XXX to XXX, but from XXX to const XXX should be OK, Why this error happens here?

推荐答案

但是从XXX到const XXX应该没问题

but from XXX to const XXX should be OK

否,在此特定情况下不行.考虑:

No, this isn't okay in this specific instance. Consider:

int const x = 10; // implementation stores to read-only memory
                  // implementation crashes on writes to read-only memory

void foo(int const **ptr) {
  *ptr = &x;
}

int main() {
  int *p;
  foo(&p);
  *p = 12; // crash
}

如果这是合法的,则会将指向const的指针"的值分配给指向非const的指针"的对象,因此可以危险地写入常量对象.

If this were legal it would assign a 'pointer to const' value to a 'pointer to non-const' object, and therefore enable dangerous writing to constant objects.

为使向const的转换正常,必须在上面添加最低const的类型中的每个级别上添加const(最顶部除外).

For a conversion to const to be okay const must be added at every level in the type above where the lowest const is added (except the very top).

例如,将int ******转换为int ***const***是不可行的,但是将>转换为int ***const*const*const*是可以的.这也适用于volatile:您可以将int ******转换为int ***volatile*const*const*,但不能转换为int ***volatile***

For example it's not okay to convert int ****** to int ***const***, but it is okay to convert it to int ***const*const*const*. This also applies to volatile: you can convert int ****** to int ***volatile*const*const* but not int ***volatile***

类型系统中的此规则可防止我们将const对象错误地视为非const,或将volatile对象错误地视为非volatile,如果我们确实要犯此错误,则必须使用const_cast.

This rule in the type system protects us from mistakenly treating const objects as non-const, or volatile objects as non-volatile, and if we really want to make this mistake then we have to use const_cast.

foo(const_cast<int const **>(&p));
*p = 12; // crash

使用const cast可以很好地构建程序,并且编译器可以愉快地生成表现出未定义行为的可执行文件. (在线示例)

With const cast the program is well formed and the compiler happily produces an executable that exhibits undefined behavior. (live example)

修复foo()以允许其获取指向非const的指针:

Fixing foo() to allow it to take a pointer to non-const:

void foo(int const * const *ptr) {
  *ptr = &x; // error, can't modify *ptr 
}

foo(&p); // conversion works fine

防止foo()将指向const的指针"值写入指向const的指针"对象. (在线示例)

prevents foo() from writing a 'pointer to const' value into a 'pointer to non-const' object. (live example)

您可能认为XXX到const XXX没关系,因为这种情况最常见,即使用单级指针:int *-> int const *没问题并且也服从以上转换规则. const最高级别无关紧要,因为对参数本身的更改不会使该函数转义.

You probably got the idea that XXX to const XXX is okay because the most common case of this, i.e. with single level pointers: int * -> int const *, is okay and also obeys the above conversion rule. const at the very top level doesn't matter because changes to the parameter itself won't escape the function.

这篇关于无法从XXX **转换为const XXX **的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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