为什么我在转换“float**”时出错到“const float**”?

发布于 2024-08-25 03:14:01 字数 319 浏览 8 评论 0原文

我有一个接收 float** 作为参数的函数,我尝试将其更改为采用 const float**

编译器(g++)不喜欢它并发出:

invalid conversion from 'float**' to 'const float**'

这对我来说毫无意义,我知道(并已验证)我可以将 char* 传递给采用 const char* 的函数,那么为什么不使用 const float** 呢?

I have a function that receives float** as an argument, and I tried to change it to take const float**.

The compiler (g++) didn't like it and issued :

invalid conversion from ‘float**’ to ‘const float**’

this makes no sense to me, I know (and verified) that I can pass char* to a function that takes const char*, so why not with const float**?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

居里长安 2024-09-01 03:14:01

请参阅 为什么在转换 Foo** → const Foo** 时出现错误?< /a>

因为转换 Foo**const Foo** 是无效且危险的......从 Foo** 转换的原因→ const Foo** 的危险在于它会让你在没有强制转换的情况下悄悄地、意外地修改 const Foo 对象

该参考文献继续给出了一个示例,说明这种隐式转换如何允许我修改 <没有强制转换的 code>const 对象。

See Why am I getting an error converting a Foo** → const Foo**?

Because converting Foo**const Foo** would be invalid and dangerous ... The reason the conversion from Foo**const Foo** is dangerous is that it would let you silently and accidentally modify a const Foo object without a cast

The reference goes on to give an example of how such an implicit conversion could allow me one to modify a const object without a cast.

萌化 2024-09-01 03:14:01

这是一个非常棘手的限制。它与语言的别名规则有关。看看标准是怎么说的,因为我以前遇到过一次:

(第61页)

[注意:如果程序可以分配
T** 类型的指针指向
输入 const T** (即 if line //1
以下是允许的),一个程序可以
无意中修改了 const 对象
(正如在 //2 行中完成的那样)。为了
例如,

int main() {
  const char c = 'c';
  字符* PC;
  const char** pcc = &pc; //1:不允许
  *pcc = &c;
  *pc = 'C'; //2: 修改一个const对象
}

—尾注]

This is a very tricky restriction. It is related to the aliasing rules of the language. Take a look at what the standards say, because I have faced this once before:

(Page 61)

[Note: if a program could assign a
pointer of type T** to a pointer of
type const T** (that is, if line //1
below was allowed), a program could
inadvertently modify a const object
(as it is done on line //2). For
example,

int main() {
  const char c = 'c';
  char* pc;
  const char** pcc = &pc; //1: not allowed
  *pcc = &c;
  *pc = 'C'; //2: modifies a const object
}

—end note]

一笑百媚生 2024-09-01 03:14:01

其他答案详细说明了为什么这是 C++ 中的错误。

让我来解答你问题背后的问题。您想在函数的接口中声明您的函数不会修改数组中包含的浮点值。良好的意图,并允许使用 const float ** 数组调用您的函数。你的问题背后的问题是,如何在不解决丑陋的演员阵容的情况下实现这一目标。

实现您想要的正确方法是将函数参数的类型更改为 const float * const * 。

星号之间的附加 const 向编译器保证您的方法不会尝试在数组中存储指向 const float 的指针,因为此类型声明指针值也是 const。

您现在可以使用 float ** (这是您问题中的示例)、const float **const float * const * 调用此函数代码> 参数。

Other anwers have detailled why this is an error in C++.

Let me address the question behind your question. You wanted to state, in the interface of your function, that your function will not modify float values contained in the array. Nice intention, and enables that your function is called with const float ** arrays. The question behind your question would be, how to achieve this without resolving to ugly casts.

The correct way to achieve what you wanted is to change the type of your function parameter to const float * const *.

The additional const between the stars assures the compiler that your method will not try to store pointers to const float in the array, since this type declares that the pointer values are also const.

You can now call this function with float ** (which was the example in your question), const float **, and const float * const * arguments.

黎夕旧梦 2024-09-01 03:14:01

如果将参数转换为 const float**,则可以将 const float* 存储在参数指向的内存位置。但是调用函数认为这个内存位置应该包含一个非常量的float*,并且稍后可能会尝试更改这个指向的float

因此,您不能将 float** 转换为 const float**,它允许您将指向常量的指针存储在需要指向可变值的指针的位置。

有关更多详细信息,请参阅 C++ FAQ Lite

If you converted the parameter to const float** you could then store a const float* at the memory location where the parameter points to. But the calling function thinks that this memory location is supposed to contain a non-const float* and might later try to change this pointed-to float.

Therefore you cannot cast a float** to a const float**, it would allow you to store pointers to constants in locations where pointers to mutable values are expected.

For more details see the C++ FAQ Lite.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文