variant类型在C++14并无加入,在cppreference网站上能够看到该类型将会在C++17加入,若想在不支持C++17的编译器上使用variant类型,咱们能够经过boost的variant类型,variant类型能够表示任意一种类型和any类型有些类似,但仍是有些区别,好比说variant支持的类型需提早定义,而any类型不须要,获取any类型的值须要给出原始类型,然而variant类型支持多种方式访问,其中一种就是经过访问者模式来访问,是不须要给出原始类型的,下面将浅谈variant的几种访问方式(我的博客也发表了《浅谈boost.variant的几种访问方式》)。html
boost::variant<int, std::string> v; v = "Hello world"; std::cout << boost::get<std::string>(v) << std::endl;
使用boost::get来访问,须要给出原始类型,而且这样作不安全,若类型错误,程序将会抛出异常。安全
void var_print(boost::variant<int, std::string>& v) { if (v.type() == typeid(int)) { std::cout << boost::get<int>(v) << std::endl; } else if (v.type() == typeid(std::string)) { std::cout << boost::get<std::string>(v) << std::endl; } // Else do nothing } int main() { boost::variant<int, std::string> v; v = "Hello world"; var_print(v); return 0; }
使用RTTI技术能够避免类型访问错误而程序异常的状况,可是这样作有点不优雅,每增长一个类型,都须要修改if-else结构,而且使用RTTI会对程序性能有必定影响。app
class var_visitor : public boost::static_visitor<void> { public: void operator()(int& i) const { std::cout << i << std::endl; } void operator()(std::string& str) const { std::cout << str << std::endl; } }; int main() { boost::variant<int, std::string> v; v = "Hello world"; boost::apply_visitor(var_visitor(), v); return 0; }
使用该模式,须要定义一个类并继承于boost::static_visitor,在类里面须要重载()
操做符,经过boost::apply_visitor来访问原始类型的值,这样作仍是有些繁琐,每增长一个类型,都须要在var_visitor里面增长一个函数,但比使用RTTI里面的修改if-else结构好得多,由于使用访问者模式至少是遵循开放-封闭原则的,即对写开放,对修改封闭。函数
class var_visitor : public boost::static_visitor<void> { public: template<typename T> void operator()(T& i) const { std::cout << i << std::endl; } }; int main() { boost::variant<int, std::string> v; v = "Hello world"; boost::apply_visitor(var_visitor(), v); return 0; }
将operator()
改为了模板函数的好处就是不用关心variant支持多少类型。性能
boost官网网站