升压变:如何获取当前持有的类型? [英] Boost Variant: how to get currently held type?

查看:129
本文介绍了升压变:如何获取当前持有的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解各类 boost.variant 被解析成真类型(意思好像提振变体LT; INT,串>一种; A =喇嘛喇嘛将编译之后转成字符串; A = 喇嘛喇嘛),所以,我想知道:如何获得什么类型的投入提升变种?

有什么我试过:

 的#include<升压/ variant.hpp>
#包括LT&;升压/ function.hpp>
#包括LT&;升压/ shared_ptr.hpp>
#包括LT&;&iostream的GT;诠释的main()
{
    TYPEDEF的boost ::功能和LT;双(双X)> func0;
    TYPEDEF的boost ::功能和LT;双(双X,双Y)> func1的;
    TYPEDEF提振::变体LT; INT,func0,func1的> variant_func;
    func1的FN =的std ::加<双>();
    variant_func V(FN);
    性病::法院LT&;<提高::获得<&func1的GT;(五)(1.0,1.0)LT;<的std :: ENDL; //这个工程
    //性病::法院LT&;<提高::获得< v型:: GT&;(五)(1.0,1.0)LT;<的std :: ENDL; //这不符合许多错误编译
    //性病::法院LT&;< (ⅴ)(1.0,1.0)下;&下;的std :: ENDL; //这个失败,错误1错误C2064:术语不计算为取2参数的函数    的std :: cin.get();
    返回0;
}


解决方案

v.which()将返回对象的当前持有的该类型的基于0的索引

当您检索对象的code必须使用静态类型(为了满足获取LT; T> 函数模板)来引用(有效)动态类型的对象。

您需要的类型或者测试(使用其中()类型())和分支相应或者使用一个静态的游客。无论您选择哪种方式,你必须明确地指出要检索的静态类型,它有相匹配的动态类型或将引发异常。

解决这个问题的方法之一是,而不是直接使用变量类型,使用含有内部变量类型的类,然后定义需要使用的对象与最低做文章任何隐式转换操作符。

我有一个名为动态C ​​++ 项目,该项目使用了这项技术。

As I understood all types of boost.variant are parsed into real types (meaning as if boost variant<int, string> a; a="bla-bla" would after compilation turn into string a; a="bla-bla") And so I wonder: how to get what type was put into boost variant?

What have I tried:

#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;
}

解决方案

v.which() will return the 0-based index of the type of the object currently held.

When you are retrieving the object your code must use a static type (in order to satisfy the get<T> function template) to refer to an (effectively) dynamically typed object.

You need to either test for the type (using which() or type()) and branch accordingly or use a static visitor. No matter which way you choose, you have to explicitly state the static type that you want to retrieve and it has to match the dynamic type or an exception will be thrown.

One way around this problem is instead of using a variant type directly, use a class which contains a variant type internally and then defines any implicit conversion operators necessary to use the object with minimum fuss.

I have a project called Dynamic C++ which uses this technique.

这篇关于升压变:如何获取当前持有的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆