Binding C union in Fortran

You can place unions or std::variant on the heap. A small example can be found here: Japanese Subgroup GENERIC proposal - #20 by ivanpribec

struct circle { float radius = 1.0; };
struct square { float width = 1.0; };

using shape = std::variant<circle,square>;

int main() {
    std::vector<shape> my_shapes;
    // ...

The vector container uses heap-allocated memory.

1 Like