lundi 27 juin 2016

Copy non-trivial c++ union

I'm trying to convert the following data structure:

template<typename ValueT, typename ChildT>
class MyUnion 
{
public:
    MyUnion() : mChild(NULL) {}
private:
    union {
        ChildT* mChild;
        ValueT* mValue;
    };
};

ValueT can be both POD (int, float, etc) and non-trivial stuff like Vec3, std::string which is the reason it was initially implemented as a pointer to dynamically allocated memory. However, with c++11 we can now store the value directly in the class. The result I'm looking for is this:

template<typename ValueT, typename ChildT>
class MyUnion 
{
public:
    MyUnion() : mChild(NULL) {}
private:
    union {
        ChildT* mChild;
        ValueT mValue;
    };
};

Changing this makes the compiler complain that the copy constructor is missing, so I want to implement

MyUnion(const MyUnion& other);
MyUnion& operator=(const MyUnion& other);

and ideally the move constructors also. Previously the compiler implemented these for me. With POD I could do a memcpy or something similar -- can I use the same now and expect correct outcome?

Aucun commentaire:

Enregistrer un commentaire