简体   繁体   中英

Call function parameters in C++11 using variadic templates

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.?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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