【发布时间】:2012-01-10 18:13:36
【问题描述】:
据我了解,boost.variant 的所有类型都被解析为真实类型(意思是好像 boost variant<int, string> a; a="bla-bla" 在编译后会变成 string a; a="bla-bla")所以我想知道:如何让什么类型被放入 boost 变体中?
我尝试了什么:
#include <boost/variant.hpp>
#include <boost/function.hpp>
#include <boost/shared_ptr.hpp>
#include <iostream>
int main()
{
typedef boost::function<double (double x)> func0;
typedef boost::function<double (double x, double y)> func1;
typedef boost::variant<int, func0, func1> variant_func;
func1 fn = std::plus<double>();
variant_func v(fn);
std::cout << boost::get<func1>(v)(1.0, 1.0) << std::endl; // this works
//std::cout << boost::get<v::type>(v)(1.0, 1.0) << std::endl; // this does not compile with many errors
// std::cout << (v)(1.0, 1.0) << std::endl; // this fails with Error 1 error C2064: term does not evaluate to a function taking 2 arguments
std::cin.get();
return 0;
}
【问题讨论】:
标签: c++ boost boost-variant