-5

I have a boost variant and I want to assign values to it. The code looks like..

boost::variant <int, std::vector<int>,std::vector<float> > MyVariant;

How can I assign values to, int, vector of ints and vector of floats. Simple assignment is not working.

Tas
  • 7,023
  • 3
  • 36
  • 51
  • 3
    Please edit your question to show what you've tried so far. The [documentation](https://www.boost.org/doc/libs/1_68_0/doc/html/variant/tutorial.html) seems fairly clear. – G.M. Sep 17 '18 at 11:18
  • what is the meaning of "not working" ? Please include the code and the error messages in the question. – 463035818_is_not_an_ai Sep 17 '18 at 11:43

1 Answers1

2

It seems pretty straightforward:

#include <boost/variant.hpp>
#include <vector>

int main()
{
    using MyVariant = boost::variant<int, std::vector<int>, std::vector<float>>;
    MyVariant m;
    m = 1;
    m = std::vector<int>{1, 2, 3};
    m = std::vector<float>{1.f, 2.f, 3.f};
    return 0;
}

With a C++17 compiler you can use std::variant instead of boost::variant.

Tas
  • 7,023
  • 3
  • 36
  • 51
Max
  • 638
  • 1
  • 4
  • 19