C++: How to create a vector storing vectors of any

2019-03-05 13:24发布

问题:

I'd like to store vectors of any type in another vector. So, for example I have two vector instances, "std::vector v1" and "std::vector v2". And I would like to put them into a vector. I already tried like this:

std::vector<int> v1;
std::vector<std::string> v2;
std::vector< std::vector<boost::any> > vc;
vc.push_back(v1);

And several other ways, but nothing works. Do you know a possible solution?

Thanks!

回答1:

Disclaimer: I don't recommend this.

But if you insist, here is the beginnings of such an abomination:

#include <vector>
#include <memory>
#include <functional>
#include <boost/any.hpp>


class any_vector
{
  using any = boost::any;
  struct concept
  {
    virtual any at(std::size_t) = 0;
    virtual any at(std::size_t) const = 0;
    virtual ~concept() = default; 
  };

  template<class T>
    struct model : concept
    {
      model(std::vector<T> v) : _data(std::move(v)) {}
      virtual any at(std::size_t i) override { return boost::any(std::ref(_data.at(i))); }
      virtual any at(std::size_t i) const override { return boost::any(std::cref(_data.at(i))); }
      std::vector<T> _data;
    };

  concept& deref() { return *vec_; }
  concept const& deref() const { return *vec_; }

  std::unique_ptr<concept> vec_;

  public:

  boost::any at(std::size_t i) const { return deref().at(i); }
  boost::any at(std::size_t i) { return deref().at(i); }

  template<class T>
  any_vector(std::vector<T> v)
  : vec_(std::make_unique<model<T>>(std::move(v)))
  {}
};

int main()
{
  any_vector a(std::vector<int> { 1, 2, 3 });
  any_vector b(std::vector<double> { 1.1, 2.2, 3.3 });

  std::vector<any_vector> va;
  va.push_back(std::move(a));
  va.push_back(std::move(b));


  auto anything = va.at(0).at(1);
  // how you deal with the resulting `any` is up to you!
}

Note that any_vector::at(x) will return a boost::any which will either contain a const-ref or a ref to some object.

Writing useful code that deduces what the thing is and uses it is going to be a challenge...