C++ 当模板参数为非POD时,为什么boost变量引发异常?

C++ 当模板参数为非POD时,为什么boost变量引发异常?,c++,variant,C++,Variant,对于POD,甚至std::string boost变体都适用于我,但当我尝试使用我的用户类型A时,以下代码失败: #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;

对于POD,甚至std::string boost变体都适用于我,但当我尝试使用我的用户类型A时,以下代码失败:

#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;
}

为什么会这样?

您的第二个示例实际上包含另一个int,因为char可以转换为int;哪一个将调用类型聚合初始化的默认构造函数之一?我不太清楚,然后用A初始化变量。

你认为A='A'选择哪种类型?提示:你不能写A='A';。