Skip to content

[bugfix] Fix default constructor of Std::variant

Carsten Gräser requested to merge feature/default-constructible-variant into master

The behaviour of the default constructor std::variant<T0, ...> is as follows:

  • (a) If T0 is default constructible, then the variant will hold a value-initialized T0 and the index is 0.
  • (b) If T0 is not default constructible, then the default constructor does npt participate in overload resolution.

In contrast to this until now the following is implemented

  • (c) The default constructor of Std::variant does alway exist and will initialize it with a special invalid state (which is BTW not copyable).

Having (c) instead of (a) especially prevented copying default constructed Std::variant<Std::monostate, ...> objects.

This patch at least ensures (a) but keeps (c) if T0 is not default constructible. The reason for the latter is, that (b) is much harder to fix and unlikely to lead to problems.

Merge request reports

Loading