首页 > 其他 > 详细

boost variant

时间:2019-06-20 20:04:27      阅读:159      评论:0      收藏:0      [点我收藏+]

Boost Variant resembles union. You can store values of different types in a boost::variant.

1.

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

int main() {
  boost::variant<double, char, std::string> v;
  v = 3.14;
  v = A;
  v = "Boost";
}

boost::variant is a template, at least one parameter must be specified. One or more template parameters specify the supported types.

2. accessing values in boost::variant with boost::get()

#include <boost/variant.hpp>
#include <string>
#include <iostream>

int main() {
  boost::variant<double, char, std::string> v;
  v = 3.14;
  std::cout << boost::get<double>(v) << std::endl;
  v = A;
  std::cout << boost::get<char>(v) << std::endl;
  v = "Boost";
  std::cout << boost::get<std::string>(v) << std::endl;
  return 0;
}

to display the stored values of v, use the free-standing function boost::get().

3. using a visitor for boost::variant

 

boost variant

原文:https://www.cnblogs.com/sssblog/p/11060811.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!