简体   繁体   English

使用可变参数模板的通用访问者

[英]Generic visitor using variadic templates

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

It would be nice to call function object "func" with variable number of arguments ( commented line ).用可变数量的 arguments 调用 function object “func”会很好(注释行)。 Do you know the easiest way to implement that?你知道实现它的最简单方法吗?

EDIT: TestVisitor is not final.编辑:TestVisitor 不是最终的。 Feel free to modify it in order to apply parameter pack of Args... to std::function call.随意修改它,以便将 Args... 的参数包应用于 std::function 调用。

This answer may help you for applying a function to a parameter pack此答案可能会帮助您将 function 应用于参数包

How do I expand a tuple into variadic template function's arguments? 如何将元组扩展为可变参数模板函数的 arguments?

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我在我的博客上发布了很多,我玩了一两天,如果你需要更多,我可以发布我的代码: Variadic Template Treatise

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM