gpt4 book ai didi

c++ - 什么是常量无效?

转载 作者:IT老高 更新时间:2023-10-28 12:01:07 25 4
gpt4 key购买 nike

std::is_void 的描述声明:

Provides the member constant value that is equal to true, if T is the type void, const void, volatile void, or const volatile void.

那么 const voidvolatile void 可能是什么?

This answer声明 const void 返回类型无效(但在 VC++ 2015 上编译)

const void foo() { }

如果按照标准,const void 是无效的(VC 是错误的) - 那么 const void 是什么?

最佳答案

const void 是一种可以形成指针的类型。它类似于普通的 void 指针,但转换的工作方式不同。例如,const int* 不能隐式转换为 void*,但可以隐式转换为 const void*。同样,如果你有一个 const void* 你不能 static_cast 它到一个 int*,但你可以 static_cast它到一个 const int*.

const int i = 10;
void* vp = &i; // error
const void* cvp = &i; // ok
auto ip = static_cast<int*>(cvp); // error
auto cip = static_cast<const int*>(cvp); // ok

关于c++ - 什么是常量无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37881240/

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