使用可变参数模板通用游客 [英] Generic visitor using variadic templates

查看:111
本文介绍了使用可变参数模板通用游客的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有小code波纹管:

I've have small code bellow:

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

struct Test
{
        void one() const { std::cout << "one\n"; }
        void two(int i) const { std::cout << "two\n"; }
};

struct TestVisitor : public boost::static_visitor<>
{
        template<class ... Args>
        void operator()( const std::function<void (Args...)>& func) const
        {
            //func(args...);
        }
};

int main() 
{ 
    Test test;
    std::function<void ()> f = std::bind(&Test::one, &test);  

    typedef boost::variant< std::function<void ()>, std::function<void (int)> > fvariant;
    fvariant var = f;
    boost::apply_visitor( TestVisitor(), var );

    return 0;
}

这将是很好调用函数对象FUNC用的参数变量数(注释行)。你知道,以实现最简单的方法是什么?

It would be nice to call function object "func" with variable number of arguments ( commented line ). Do you know the easiest way to implement that?

编辑:TestVisitor不是最终的。随意修改,以便应用参数数量的参数包...到std ::函数调用。

TestVisitor is not final. Feel free to modify it in order to apply parameter pack of Args... to std::function call.

推荐答案

我张贴在这个有很多在我的博客,我发挥它周围一两天,如果你需要更多的,我可以发表我的code:
可变参数模板伤寒

I posted a lot on this on my blog, I played around with it for a day or two, if you need more, I can post my code: Variadic Template Treatise

这篇关于使用可变参数模板通用游客的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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