News for hackers. Stuff that matters.
Re:Switching from C++ to Rust by dilap on Monday March 13, 2023 @09:04PM (#35145855)
i haven't done c++ in a million years, but huh, you can! can't have any types w/ non-trival copy constructors in a union, though, apparently, which is quite the restriction.

https://gist.github.com/erinok/c823af95db408653c7e42ab189307...

Re:Switching from C++ to Rust by ivmaykov on Monday March 13, 2023 @09:26PM (#35146069)
I may be wrong, but I thought it’s that you can’t have any fields with non trivial destructors, because the union wouldn’t know which destructor to call. So POD types / raw pointers / arrays of the above / structs of the above are allowed, and that’s pretty much it.
Re:Switching from C++ to Rust by gpderetta on Monday March 13, 2023 @10:33PM (#35146627)
You can have non trivial types in an union, but you need to explicitly define constructor in the union or the containing type (i.e. the default union copy constructor and assignment operator are disabled).
Re:Switching from C++ to Rust by jcranmer on Tuesday March 14, 2023 @12:24AM (#35147432)
> can't have any types w/ non-trival copy constructors in a union, though, apparently, which is quite the restriction.

That was removed in C++11. The rule now is:

> Absent default member initializers ([class.mem]), if any non-static data member of a union has a non-trivial default constructor ([class.default.ctor]), copy constructor, move constructor ([class.copy.ctor]), copy assignment operator, move assignment operator ([class.copy.assign]), or destructor ([class.dtor]), the corresponding member function of the union must be user-provided or it will be implicitly deleted ([dcl.fct.def.delete]) for the union.

i.e., you need to provide an explicit version of the special function for the union if any member has a nontrivial implementation.

Re:Switching from C++ to Rust by dilap on Tuesday March 14, 2023 @12:38PM (#35154258)
ah, good to know, thanks! unfortunately too late to edit my comment