-1

For POD and even std::string boost variant works for me, but when I try my user type A, it fails for this code:

#include "stdafx.h"
#include <boost/variant/variant.hpp>
#include <boost/variant/get.hpp>

struct A
{
  char ch;
};

int main()
{
  boost::variant< int, A > n, a;
  n = 33;
  a = 'a';

  try
  {
    int nn = boost::get< int >( n ); // ok
    auto aa = boost::get< A >( a ); // throws bad_get
  }
  catch( boost::bad_get& )
  {
    bool okay = false;
  }

  return 0;
}

Why is that?

rtischer8277
  • 496
  • 6
  • 27

1 Answers1

2

Your second example actually holds another int since char can be converted to int. To construct an A from a char you can do a = A{'a'}; which will call one of the default constructors for your type (aggregate initialization perhaps? I don't know exactly) and then that initializes your variant with an A.

SirGuy
  • 10,660
  • 2
  • 36
  • 66