什么是常量虚空的地步? [英] What's the point of const void?
问题描述
显然,这是可能的声明函数返回常量无效
:
Apparently, it is possible to declare a function returning const void
:
const void foo()
{
}
G ++似乎考虑常量
重要,因为下面的code不能编译:
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");
也是如此常量无效
有什么实际意义?
推荐答案
不是真的。但忽略的 CV 的-qualifications对无效或使他们错误可能会在两种编译器实现和最终用户code方面产生不必要的复杂性。考虑像
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 ...
有没有理由让使用虚空中这种情况特殊情况下(超过它已经是),它只是创造头疼的问题。
There's no reason to make using void in that scenario a special case (more than it already is), it would just create headaches.
此外,虽然常量无效
是没有帮助的,常量无效*
有它的用途。
Also, while const void
isn't helpful, const void*
has its uses.
这篇关于什么是常量虚空的地步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!