Domanda

I'm trying to create an anonymous union that holds a generic vector in order to use it as a member in a class without naming the type_name of the union itself.

So that I could call the vector inside the class as following:

vec.size();

But my approach

template <typename T>
union{
  std::vector<T> vec;
};

will only give me the error "template class without a name". This also happens with structures. So does it not like to be anonymous when it is generic?

Search results just gave me the option to create a generic vector inside a named structure but, besides I couldn't get this to work either, I would loose the benefits of the anonymous union und I would need to call the vector e.g. as

struct_name.vec.size();

or even

class_name.struct_name.vec.size();

which I tried to avoid.

I want to make the vector generic so that it can store integers or doubles and I don't need to declare two different vectors with their own specific data types. Beside learning some principles of generics I also aim for lesser declarations and storage usage with this technique.

È stato utile?

Soluzione

You cannot reliably do what you want. You need at least some way to discriminate at runtime if you have a vector of int or a vector of float.

With C++11 you might code

class StrangeVector {
    bool has_int;
    union {
        std::vector<int> vint;
        std::vector<float> vfloat;
    };
public:
    StrangeVector(bool withints) : has_int(withints) {
        if (withints) new(&vint) std::vector<int>();
        else new(&vfloat) std::vector<float>();
    }
    ~StrangeVector() {
        if (has_int) vint.~vector<int>();
        else vfloat.~vector<float>();
    }
};

But such code is really bad smelling. (I would suggest using a union of pointers, perhaps of smart pointers e.g. std::shared_ptr or std::unique_ptr, or perhaps std::optional; see this).

See also this, or boost::any ...

Notice that except for RTTI typing information is used at compile time in C++.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top