1

When passing a boost::function as a parameter to another function (callback), this function's signature can become quite long.

Example:
Consider this boost::function:

boost::function<MyClass * (
      TypeA param1,
      TypeB param2,
      TypeC param3,
      TypeD param4,
      TypeE param5,
      TypeF param6)> CreateMyClass;

Now if we pass this boost::function as a function parameter, the signature, of the function using it, becomes horribly long and hard to read:

void myFunctionUsingTheCallack(boost::function<MyClass * (
          TypeA param1,
          TypeB param2,
          TypeC param3,
          TypeD param4,
          TypeE param5,
          TypeF param6)> the_callback);

Am I missing something here? Is there any trick to shorten the signature of myFunctionWithTheCallack?

gpalex
  • 826
  • 1
  • 11
  • 27

2 Answers2

5

Why don't you typedef to a shorter name?

typedef boost::function<MyClass * (
  TypeA param1,
  TypeB param2,
  TypeC param3,
  TypeD param4,
  TypeE param5,
  TypeF param6)> Fun;

Fun CreateMyClass;

void myFunctionUsingTheCallack(Fun the_callback);

If you were using C++11 or above, you could use using instead of typedef:

using Fun = boost::function<MyClass * (
  TypeA param1,
  TypeB param2,
  TypeC param3,
  TypeD param4,
  TypeE param5,
  TypeF param6)>;

Instead of explicitly specifying the type you could also use a function template and let the type be deduced by the compiler:

template <typename Fun>
void myFunctionUsingTheCallack(Fun the_callback);
m.s.
  • 16,063
  • 7
  • 53
  • 88
  • 1
    You should add the `using` version, just for completeness for future readers. – nwp Aug 11 '15 at 11:15
0

Another alternative, on top of the two posted by @m.s. (use a concrete API to get the type):

// defined elsewhere:
void some_api(TypeA param1,
    TypeB param2,
    TypeC param3,
    TypeD param4,
    TypeE param5,
    TypeF param6);

using native_callback_t = decltype(&some_api);
void myFunctionUsingTheCallack(std::function<native_callback_t> callback);
utnapistim
  • 26,809
  • 3
  • 46
  • 82
  • 1
    that would require C++11 which *probably* is not available, otherwise why would the OP use `boost::function`? – m.s. Aug 11 '15 at 11:07
  • 1
    @m.s. Some people use boost for everything and wonder why there is no `boost::vector`. – nwp Aug 11 '15 at 11:13