gpt4 book ai didi

c++ - 为什么从 char*** 到 char*const** 的转换无效?

转载 作者:IT老高 更新时间:2023-10-28 22:19:27 26 4
gpt4 key购买 nike

根据N4606 , 4.5 [conv.qual] 第 3 段内容为

A prvalue expression of type T1 can be converted to type T2 if the following conditions are satisfied, where cvij denotes the cv-qualifiers in the cv-qualification signature of Tj:

  • ...
  • If the cv1i and cv2i are different, then const is in every cv2k for 0 < k < i.

上面的最后一个项目符号表明以下转换失败。

T1 : pointer to / pointer to /       pointer to / T
T2 : pointer to / pointer to / const pointer to / T

为了成功,T2必须是pointer to/const pointer to/const pointer to/TT2 是否足以比 T1 更有 cv 限定? 为什么转换为成功了吗?

最佳答案

考虑以下代码:

char str[] = "p1 should always point here.";
char* const p1 = str;
char** p2 = nullptr;
char*** p3 = &p2;

char str2[] = "Can we make p1 point to here?"
// p1 = str2; // does not work: const violation

// But:
char*const** p4=p3; // if it were allowed
*p4 = &p1; // no const violation, as *p4 is of type char*const*
**p3 = str2; // oops, changed p1!

因此,如果允许相关转换,您将可以更改一个常量变量 (p1),而不会违反任何正式的 const。

关于c++ - 为什么从 char*** 到 char*const** 的转换无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40200807/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com