Rodhos Soft

備忘録を兼ねた技術的なメモです。Rofhos SoftではiOSアプリ開発を中心としてAndroid, Webサービス等の開発を承っております。まずはご相談下さい。

variant 共用体

かなり便利なことがわかった。

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

int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    
    boost::variant<int, std::string> v;
    v = 100;

    boost::variant<int, std::string> s;
    s = "200";

    if (v.type() == typeid(int)) {
        int l = boost::get<int>(v);
        std::cout << l + 1000 << std::endl;
        
        int* k = boost::get<int>(&v);
        std::cout << *k + 2000 << std::endl;
        
    }
    
    return 0;
}