How much existing C++ code would break if void was actually defined as `struct void {};`

北战南征 提交于 2019-11-29 09:09:04

There is a proposal for this, it is p0146: Regular Void

Presented below is a struct definition that is analogous to what is proposed for void in this paper. The actual definition is not a class type, but this serves as a fairly accurate approximation of what is proposed and how developers can think about void. What should be noticed is that this can be thought of as adding functionality to the existing void type, much like adding a special member function to any other existing type that didn't have it before, such as adding a move constructor to a previously non-copyable type. This comparison is not entirely analogous because void is currently no ordinary type, but it is a reasonable, informal description, with details covered later.

struct void {
  void() = default;
  void(const void&) = default;
  void& operator =(const void&) = default;

  template <class T>
  explicit constexpr void(T&&) noexcept {}
};

constexpr bool operator ==(void, void) noexcept { return true; }
constexpr bool operator !=(void, void) noexcept { return false; }
constexpr bool operator <(void, void) noexcept { return false; }
constexpr bool operator <=(void, void) noexcept { return true; }
constexpr bool operator >=(void, void) noexcept { return true; }
constexpr bool operator >(void, void) noexcept { return false; }

It was received well in Oulu June 2016 meeting Trip Report:

Regular void, a proposal to remove most instances of special-case treatment of void in the language, making it behave like any other type. The general idea enjoyed an increased level of support since its initial presentation two meetings ago, but some details were still contentious, most notably the ability to delete pointers of type void*. The author was encouraged to come back with a revised proposal, and perhaps an implementation to help rule out unexpected complications.

I chatted with the author and he confirmed that it is basically waiting for an implementation, once there is an implementation he plans on bringing the proposal back.

There is extensive discussion in the paper about what changes and why, it is not really quotable as a whole but the FAQ questions addressed are:

  • Doesn't This Proposal Introduce More Special-Casing for void?
  • Why Isn't sizeof(void) Equal to 0?
  • Does This Break std::enable_if?
  • In Practice, Would This Break ABI Compatibility?
  • Doesn't constexpr_if Make Branching for void Easier?
  • Isn't It Illogical to Support some-operation for void?
  • Doesn't This Remove the Notion of "No Result?"
  • Isn't This a Change to the Meaning of void?
einpoklum
  • void is a type with an empty domain (it has not possible values);
  • struct foo { } is a type with a non-empty domain (there is one value of this type).

In other words, void is a bottom type while a potential struct void {} would be a unit type.

Replacing the first with second essentially breaks, well, the whole world. It's not entirely dissimilar from deciding that 0 equals 1 in C++.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!