boost.variant 的使用以及几种访问方法

#include<iostream>
#include<cstdlib>
#include<string>
#include<vector>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <boost/filesystem.hpp>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
#include <typeinfo>
#include <algorithm>
#include <list>
#include <set>
#include <future>
using namespace std;
#include<iostream>
#include<thread>
#include <mutex>
#include <signal.h>
#include <fstream>
#include <condition_variable>
using namespace std;
 #include <ctime>
    #include <iostream>
    #include <string>
     #include <iostream>
    #include <boost/array.hpp>
    #include <boost/asio.hpp>
    #include <boost/asio.hpp>
    #include <signal.h>
    #include <boost/optional.hpp>
    #include <bits/stdc++.h>
    #include <boost/optional.hpp>
    #include <iterator>
    #include <memory>
  #include <boost/program_options.hpp>
  #include <boost/variant.hpp>
    using namespace std;
using namespace boost::filesystem;
namespace po=boost::program_options;
typedef boost::variant<int,double,string> ids_t;

//通过apply_vist 使用访问者模式 ,每增加一个类型,都需要在show_visitor里面增加一个函数,
//但比使用RTTI里面的修改if-else结构好得多,因为使用访问者模式至少是遵循开放-封闭原则的,即对写开放,对修改封闭。
class show_visit:public boost::static_visitor<>
{
  public:
    void operator()(const int i) const
    {
      cout<<i<<endl;
    }
    void operator()(const double d) const
    {
      cout<<d<<endl;
    }
    void operator()(const string& s)
    {
      cout<<s<<endl;
    }
};
//两个 variant add相加
class double_visit:public boost::static_visitor<ids_t>
{
  public:
  template<class T>
  ids_t operator()(T& t1,T& t2)
  {
    return t1+t2;
  } 
  template<class T1,class T2>
  ids_t operator()(T1& t1,T2& t2)
  {
    throw exception();
  }
};
//自己加自己
class own_plus:public boost::static_visitor<ids_t>
{
  public:
   template<class T>
    ids_t operator()(T& t)
    {
      return t+=t;
    }

};

int main(int argc,char* argv[])
{
  boost::variant<int,double,string> va;//默认初始化
  cout<<va.which()<<endl;
  cout<<boolalpha<<va.empty()<<endl;//默认初始化,一直为false
  va=123;
  int a=boost::get<int>(va);//获取当前值
  cout<<va.which()<<":"<<a<<endl;//0,以0为小标
  va=12.3;
  double d=boost::get<double>(va);//1
  cout<<va.which()<<":"<<d<<endl;
  va="hello world";
  string s=boost::get<string>(va);//2
  cout<<va.which()<<":"<<s<<endl;
  boost::variant<boost::blank,int,double,string> va1;
  if(va1.which()==0)//判断是否为空的方式
  {
    cout<<"empty..."<<va1.type().name()<<endl;  
  }
  boost::variant<int,double,string> va2;
  va2="da jia hao";
  //va2=va2+va;
  show_visit sv;
  va2.apply_visitor(sv);//通过apply_visit打印值
  boost::apply_visitor_delayed_t<show_visit> show_v=boost::apply_visitor(sv);
  show_v(va2);//宁外一种方式
  double_visit dv;
  auto sd=boost::apply_visitor(dv,va,va2);//va相加
  show_v(sd);
  sd.apply_visitor(sv);

  own_plus _plus;
  sd.apply_visitor(_plus).apply_visitor(sv);

  return 0;
}

https://blog.csdn.net/janeqi1987/article/details/78425454

https://blog.csdn.net/redenval/article/details/78298680

posted @ 2022-02-15 17:33  白伟碧一些小心得  阅读(572)  评论(0编辑  收藏  举报