News for hackers. Stuff that matters.
Re:Enum class improvements for C++17, C++20 and C++23 by nly on Sunday August 04, 2024 @08:38PM (#41157287)
A few code snippets of what you see as weaknesses of std::variant may be appropriate, as I couldn't figure out your complaint. Assigning to a variant taken by non-const& works fine for me.

I personally would have liked to see recursive variant types and multi-visitation (as supported by boost::variant).

Re:Enum class improvements for C++17, C++20 and C++23 by amluto on Sunday August 04, 2024 @08:47PM (#41157326)
I think the comment means:

    std::variant<int&, etc>
does not work well.
Re:Enum class improvements for C++17, C++20 and C++23 by nly on Sunday August 04, 2024 @08:57PM (#41157372)
Just use

    std::variant<int*, ...>
References in C++ are just sugary pointers.
Re:Enum class improvements for C++17, C++20 and C++23 by robmccoll on Sunday August 04, 2024 @09:59PM (#41157589)
References have one important property over pointers. They cannot be null.
Re:Enum class improvements for C++17, C++20 and C++23 by tstack on Sunday August 04, 2024 @10:35PM (#41157704)
They most certainly can be null as can “this”.
Re:Enum class improvements for C++17, C++20 and C++23 by usefulcat on Sunday August 04, 2024 @11:10PM (#41157844)
Only after dereferencing a null pointer, which would be the actual problem, not the fact that some reference or this is null.
Re:Enum class improvements for C++17, C++20 and C++23 by Matheus28 on Monday August 05, 2024 @01:12AM (#41158328)
You cannot have a null reference unless you invoke undefined behavior first.
Re:Enum class improvements for C++17, C++20 and C++23 by cma on Monday August 05, 2024 @02:29AM (#41158607)
const ref lifetime extension is important to, or operator overloading wouldn't be workable.
Re:Enum class improvements for C++17, C++20 and C++23 by gpderetta on Monday August 05, 2024 @02:58AM (#41158733)
Reference_wrapper then.
Re:Enum class improvements for C++17, C++20 and C++23 by LowLevelMahn on Monday August 05, 2024 @02:59AM (#41158741)
they easily can :)

void test(int& y){}

int main() { int* x = nullptr; test(*x); }

Re:Enum class improvements for C++17, C++20 and C++23 by josefx on Monday August 05, 2024 @03:47AM (#41158974)
There is a difference between an API promissing that a value wont be null and a buggy program setting a null where it should not. A reference is only null if someone fucked up. As a programmer you can usually rely on a reference not being null and you couldn't do anything about it if it was anyway within the constraints of the language.
Re:Enum class improvements for C++17, C++20 and C++23 by TeMPOraL on Monday August 05, 2024 @04:50AM (#41159288)
In the same way, an `enum class` variable's value being outside of the set defined in the `enum class` is also a fuckup.
Re:Enum class improvements for C++17, C++20 and C++23 by gpderetta on Monday August 05, 2024 @05:44AM (#41159553)
no, deferencing a null ptr is UB. An enum class outside the declared values is perfectly valid.

You could design a language feature where integer to enum is checked, but that's not enum.

Enum classes already add scoping, forbid implicit conversions and allow explicit underlying types. Those are pure extensions. Making undeclared values invalid or UB would be very surprising to people used to normal enums.

Re:Enum class improvements for C++17, C++20 and C++23 by account42 on Monday August 05, 2024 @04:26AM (#41159169)
And you can also open /proc/self/mem in a Rust program and overwrite whatever you want, including pointers. So?
Re:Enum class improvements for C++17, C++20 and C++23 by Thiez on Monday August 05, 2024 @06:35AM (#41159831)
One of those cases happens accidentally all the time (in more complex variants than the motivating example you responded to), the other never happens except on purpose. It's like complaining guard rails are pointless because people being launched with catapults might still fly over them and plunge to their deaths.
Re:Enum class improvements for C++17, C++20 and C++23 by consteval on Monday August 05, 2024 @01:49PM (#41163591)
I've never seen a nullptr somehow sneak into a reference. Never.

What I have seen is automatic variables escaping their scope as a reference, which rust protects against. And is also much more dangerous, because dereferencing a nullptr is defined behavior on most platforms

Re:Enum class improvements for C++17, C++20 and C++23 by tlhand on Monday August 05, 2024 @06:17AM (#41159713)
std::variant is not a true algebraic data type, since the individual element constructors do not construct the variant type automatically. Compare to OCaml, written in a verbose and unidiomatic way that is similar to C++:

  # type foo = Int of { n : int } | Float of { f : float };;
  type foo = Int of { n : int; } | Float of { f : float; }
  # Int { n = 10 };;
  - : foo = Int {n = 10}
  # let r = ref (Int { n = 10 });;
  val r : foo ref = {contents = Int {n = 10}}
Notice that the constructor Int { n = 10 } automatically produces a foo type and assigning to a mutable ref works.

The same in C++, using assignment to a pointer to avoid the lvalue ref error that is irrelevant to this discussion:

  #include <variant>
  
  struct myint {
    int n;
    myint(int n) : n(n) {}
  };

  struct myfloat {
    float f;
    myfloat(float f) : f(f) {}
  };

  using foo = std::variant<myint, myfloat>;

  int
  main()  
  {
    const foo& x = myint{10}; // works
    foo *z = new myint{10}; // error: cannot convert ‘myint*’ to ‘foo*
  }

As stated above, this obviously cannot work since C++ has no way of specifying a myint constructor that -- like in OCaml -- automatically produces the variant type foo.

C++ would need true algebraic data types with compiler support (that would hopefully be as fast as switch statements). To be useful, they would need a nice syntax and not some hypothetical abomination like:

  using foo = std::variant<myint, myfloat> where
  struct myint of foo { ... };