What#39;s the point of const void?(const void 有什么意义?)
问题描述
显然,可以声明一个返回const void的函数:
Apparently, it is possible to declare a function returning const void:
const void foo()
{
}
g++ 似乎认为 const 很重要,因为以下代码无法编译:
g++ seems to consider the const important, because the following code does not compile:
#include <type_traits>
static_assert(std::is_same<void(), const void()>::value, "const matters");
那么const void有什么实际意义吗?
So does const void have any practical significance?
推荐答案
不是.但是忽略 void 上的 cv 限定或使其出错可能会在编译器实现和最终用户代码方面造成不必要的复杂性.考虑像
Not really. But to ignore cv-qualifications on void or to make them errors could create unnecessary complexity in terms of both compiler implementation and end-user code. Consider templates like
template<typename T>
const T ...
没有理由在这种情况下使用 void 成为一种特殊情况(比现在更多),它只会让人头疼.
There's no reason to make using void in that scenario a special case (more than it already is), it would just create headaches.
此外,虽然 const void 没有帮助,但 const void* 也有其用处.
Also, while const void isn't helpful, const void* has its uses.
这篇关于const void 有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:const void 有什么意义?
基础教程推荐
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- c++ STL设置差异 2022-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
