0

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.

timrau
  • 22,578
  • 4
  • 51
  • 64
  • 1
    I have no idea what you're trying to accomplish. What is the problem with `std::vector`?, why does it need to be in a containing object (structure or union)? What is your perceived benefit of doing so? Do you need one instance of `std::vector<>` to hold different types (using a variant or something similar)? – Chad Feb 03 '14 at 20:10
  • 1
    How do you plan to instantiate the template? – David Heffernan Feb 03 '14 at 20:10
  • It can not be generic if it is written as `template vector vec;` As far as I understood the template must be declared outside of the braces. I want the template to be either integer or double to fill the vector with Fibonacci numbers. So for every new or following number I could write `vec.push_back(fibonacci_function());`. – user3267181 Feb 03 '14 at 20:17

1 Answers1

1

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++.

rustyx
  • 80,671
  • 25
  • 200
  • 267
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Another option would be a templated impl ptr which implements an interface, which would be more scaleable to more types. – IdeaHat Feb 03 '14 at 20:26
  • If I make my vector generic it, wouldn't it know I want to store integers when I call it like `vec`? But thank you for your quick answer! Apologize for my primitive questions, just started few weeks ago. – user3267181 Feb 03 '14 at 20:34