0

I would like to store function pointers in a boost::variant type. This is clear to me, including the creation and use of a visitor object. In the documentation of boost::variant I've stumbled on boost::visitor_ptr. The documentation gives the impression that this could simplify using the stored pointers to execute my functions. Unfortunately, there is no example or any additional documentation, and the tests in the boost source distribution do not include any code relating to boost::visitor_ptr.

Google shoots a blank. Could someone please elucidate the use of boost::visitor_ptr, if possible with a simple example?

degski
  • 642
  • 5
  • 13
  • I think `visitor_ptr` and `visitor_ptr_t` are orthogonal to what you want to do; they allow you to pass a function pointer to e.g. `boost::apply_visitor`. – melak47 Feb 17 '16 at 09:20
  • So, if you have a free function you want to apply, instead of `struct my_visitor : boost::static_visitor { stuff operator()(whatever w) const { return free_func(w); } }; boost::apply_visitor(my_visitor{}, variant);`you can use this convenience class: `boost::apply_visitor(boost::visitor_ptr(free_func), variant);` – melak47 Feb 17 '16 at 09:25
  • @melak47 I have to digest what you wrote in your second comment. I understand your answer. You return a a value from operator(), which a did not appreciate was possible (as not mentioned in examples). I'll see what this could bring to the table and get back to you. – degski Feb 17 '16 at 09:36
  • @melak47 Let's say the free_func is has a signature like `int add ( int a, int b )`, what would in this case `whatever w` be in the visitor? – degski Feb 17 '16 at 10:07
  • `visitor_ptr` does not look like it supports binary or n-ary visitation. – melak47 Feb 17 '16 at 12:58
  • @melak47 Well, clang claims it (`free_func()`, actually `my_visitor::operator()`) needs 1 argument. – degski Feb 17 '16 at 13:16

0 Answers0