3

Following on @Xeo's excellent code:

I'm wondering how to template the function's return type, so that instead of interpreting the return through:

std::string* out_opt

it could instead return the appropriate type for a given function.

Also, following on @Xeo's example code, I wondering what the best way to bind member functions that have multiple arguments would be...

func_dict["someFunc"] = stream_function<int(int,int)>( std::bind( &MyClass::functionName, instanceOfMyClass, std::_1, std::_2 ) );

Is this a good approach? Is there any way to do away with needing to explicitly state _1, _2, etc.?

Community
  • 1
  • 1
pt3dNyc
  • 369
  • 1
  • 16
  • Do you want all your functions to have the same returntype (just not returning through `string*`? That should be relatively easy. If you don't want this, you would require the caller to statically know the returntype of the function, thus making the whole dispatch mechanism kind of pointless (not very useful, if you need to know what you are calling beforehand) and much harder to implement, since the function objects would have different types. – Grizzly Dec 09 '12 at 14:14
  • No, I don't want them to all return the same type. I'm looking to generalize the code so that the function object can have different types. Why isn't that mechanism useful? – pt3dNyc Dec 09 '12 at 14:31
  • You could return a boost::any instead of going through the string outparam thing, to avoid converting to and from a string representation of the value when calling functions, but it wouldn't be much of an improvement since the caller would need to know the types ahead of time in order to cast the boost::any. – Reuben Morais Dec 09 '12 at 14:45
  • 1
    @pt3dNyc: If you want to get a return value from a function you need to know what type to expect. This means that you need to know which function you want to call (if they have different return types) beforehand. If you know the function you want to call, what do you need the whole dispatch mechanism for. As a sidenote: you might want to start your comment with `@Username`, if you write in reaction to a previous comment, since that will notify the addressed user, leading to faster reaction times. – Grizzly Dec 09 '12 at 14:50
  • @Grizzly, I see your point. I was looking at: http://loki-lib.cvs.sourceforge.net/loki-lib/loki/include/loki/Functor.h?view=markup which does implement return types, but it may not be very useful for the reasons you've pointed out. – pt3dNyc Dec 09 '12 at 22:04

1 Answers1

0

If this is C++11, why not

func_dict["someFunc"] = stream_function<int(int,int)>( 
    [&](int x, int y)
    {
      MyClass::functionName(instanceOfMyClass, x, y);
    });

Otherwise, I think you'll always need to explicitly state the arguments, one way or the other.

Barney Szabolcs
  • 11,846
  • 12
  • 66
  • 91
  • is the above correct? I'm getting a syntax error on "[&]" (expression expected). Can you point me to some documentation of this syntax? Thanks. – pt3dNyc Dec 09 '12 at 21:58
  • @pt3dNyc that means your compiler doesn't support lambdas. Try it on a recent Clang version. – Reuben Morais Dec 09 '12 at 22:19