2

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 ). Do you know the easiest way to implement that?

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

spraff
  • 32,570
  • 22
  • 121
  • 229
sigidagi
  • 864
  • 7
  • 17
  • 1
    The question as posed is a bit undercooked. If `func` is a `function` you will need a parameter pack of `Args...` to call it with. But you have not provided any such. Where should they come from? – Lambdageek Jun 28 '11 at 13:22
  • parameter pack of Args could be applied as TestVisitor constructor parameters, or as a additional parameters to function call operator, or ... I'm nor sure which way is a best way,and how to do it. – sigidagi Jun 28 '11 at 13:57

2 Answers2

0

This answer may help you for applying a function to a parameter pack

How do I expand a tuple into variadic template function's arguments?

Community
  • 1
  • 1
David
  • 3,324
  • 2
  • 27
  • 31
0

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

norcalli
  • 1,215
  • 2
  • 11
  • 22