0
struct Dingus {
union {
    int dingly[4 *4];
    vec3 dinglyDo;
}diddly;
inline Dingus() {}
};

This code produces the error

error C2280: 'Dingus::<unnamed-type-diddly>::<unnamed-type-diddly>(void)': attempting to reference a deleted function

Oddly, when I delete the "diddly" which was giving a reference to the union, there is no error.

The vec3 is a struct from the GLM library, I can replace the type with some other classes and i'll get the same error... but if I replace it with something simple like float I don't get the error

Since removing the "diddly" removes the error, this seems to be a different question than this one

Community
  • 1
  • 1
Thomas
  • 6,032
  • 6
  • 41
  • 79
  • @LogicStuff It's a struct from the GLM library, I can replace the type with some other things and i'll get the same error... but if I replace it with something simple like float I don't get the error – Thomas Jul 05 '16 at 19:20
  • Possible duplicate of [C++11 anonymous union with non-trivial members](http://stackoverflow.com/questions/10693913/c11-anonymous-union-with-non-trivial-members) – PcAF Jul 05 '16 at 19:30
  • What does _"delete the "diddly", thereby giving a name to reference the union with"_ mean? You changed `union { ... } diddly;` to `union Foo { ... };`? Because if so, that means something completely different, so the `Dingus` constructor doesn't try to default-initialize an object of that type, and so it's not odd at all that it compiles. – Jonathan Wakely Jul 05 '16 at 19:49
  • @JonathanWakely sorry I made it more clear. I meant to say, when I turn `union { ... } diddly;` into `union { ... }`, there is no error – Thomas Jul 05 '16 at 19:51

2 Answers2

3

You've declared a member of that anonymous union type, and so the member needs to be initialized in the Dingus constructor. Because the union has a member of non-trivial type it has no default constructor, so it can't be initialized in the Dingus constructor.

You can define a constructor for the union type which says what should happen when it's default-constructed e.g.

struct Dingus {
  union U {
    int dingly[4 *4];
    vec3 dinglyDo;
    U() : dingly() { }
  } diddly;
  inline Dingus() {}
};
Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
  • @KerrekSB Maybe this comment is what pushed them over the edge. [variant](http://en.cppreference.com/w/cpp/utility/variant) – Fantastic Mr Fox Jan 29 '18 at 04:36
  • 2
    @FantasticMrFox: Yes. The committee was undedicdedly puzzling over whether to spend time on exploring new directions that were being floated at the time, such as "nodules", "percepts" and "unicorn call syntax", but when this comment came in, everything was dropped and national bodies aligned behind the Convener in Chief and started sending in paper after paper in support of variant. Some warned that we'd we working on variant for the next twenty years. There was laughter. – Kerrek SB Jan 29 '18 at 10:50
0

As of c++17 you can use std::variant instead of a union to solve this problem. Your code could easily be replaced by:

struct Dingus {
    std::variant<std::array<int, 4*4>, vec3> diddly;
    inline Dingus() {}
};
Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175