4
votes

I'm currently reading a few books to get caught up on c++14 features. I am trying to use a variadic template to bind arguments to a function. I know how to do this using std::bind, but I would also like to implement this function with a c++14 lambda expression, just for common knowledge and understanding, and for any possible performance benefits. I've read that lambdas can be inlined while std::bind cannot inline because it takes place through a call to a function pointer.

Here is the code from myFunctions.h:

#include <functional>

int simpleAdd(int x, int y) {
    return x + y;
}

//function signatures
template<class Func, class... Args>
decltype(auto) funcBind(Func&& func, Args&&...args);

template<class Func, class... Args>
decltype(auto) funcLambda(Func&& func, Args&&...args);

/////////////////////////////////////////////////////////////////

//function definitions
template<class Func, class... Args>
inline decltype(auto) funcBind(Func&& func, Args&&... args)
{
    return bind(forward<Func>(func), forward<Args>(args)...);
}

template<class Func, class ...Args>
inline decltype(auto) funcLambda(Func && func, Args && ...args)
{   //The error is caused by the lambda below:
    return [func, args...]() {
        forward<Func>(func)(forward<Args>(args)...);
    };
}

Here is the main code I am running:

#include<iostream>
#include<functional>
#include "myFunctions.h"
using namespace std;


int main()
{
    cout << "Application start" << endl;
    cout << simpleAdd(5,7) << endl;

    auto f1 = funcBind(simpleAdd,3, 4);
    cout << f1() << endl;

    //error is occurring below
    auto f2 = funcLambda(simpleAdd, 10, -2);
    cout << f2() << endl;

    cout << "Application complete" << endl;

Error C2665 'std::forward': none of the 2 overloads could convert all the argument types

Error C2198 'int (__cdecl &)(int,int)': too few arguments for call

I think the error might be occurring when the variadic arguments are getting forwarded to the lambda, but I'm not really sure.

My question is how do I properly formulate this code so that I can use a lambda to capture the function and its arguments, and call it later.

2

2 Answers

5
votes

I've read that lambdas can be inlined while std::bind cannot inline because it takes place through a call to a function pointer.

If you pass simpleAdd to something that then binds the arguments, then whether you use bind or not doesn't matter. What do you think the lambda captures with func? It's a function pointer.

The lambda-vs-function-pointer case is about writing bind(simpleAdd, 2, 3) vs. [] { return simpleAdd(2, 3); }. Or binding a lambda like [](auto&&...args) -> decltype(auto) { return simpleAdd(decltype(args)(args)...); } vs. binding simpleAdd directly (which will use a function pointer).


In any event, implementing it is surprisingly tricky. You can't use by-reference capture because things can easily get dangling, you can't use a simple by-value capture because that would always copy the arguments even for rvalues, and you can't do a pack expansion in an init-capture.

This follows std::bind's semantics (invoking the function object and passing all bound arguments as lvalues) except that 1) it doesn't handle placeholders or nested binds, and 2) the function call operator is always const:

template<class Func, class ...Args>
inline decltype(auto) funcLambda(Func && func, Args && ...args)
{   
    return [func = std::forward<Func>(func), 
            args = std::make_tuple(std::forward<Args>(args)...)] {
        return std::experimental::apply(func, args);
    };
}

cppreference has an implementation of std::experimental::apply.

Note that this does unwrap reference_wrappers, like bind, because make_tuple does it.

Your original code breaks down because args are const in the lambda's function call operator (which is const by default), and the forward ends up attempting to cast away constness.

1
votes

You use a tuple:

template<class Func, class ...Args>
inline decltype(auto) funcLambda(Func && func, Args && ...args)
{   //The error is caused by the lambda below:
    auto tpl = make_tuple(std::forward<Args>(args)...);

    //Use move just in case Args has move-only types.
    return [func, tpl = move(tpl)]() {
        apply(func, tpl);
    };
}

Where apply is defined something like this:

namespace detail {
template <class F, class Tuple, std::size_t... I>
constexpr decltype(auto) apply_impl( F&& f, Tuple&& t, std::index_sequence<I...> )
{
  return f(std::get<I>(std::forward<Tuple>(t))...);
}
} // namespace detail

template <class F, class Tuple>
constexpr decltype(auto) apply(F&& f, Tuple&& t)
{
    return detail::apply_impl(std::forward<F>(f), std::forward<Tuple>(t),
        std::make_index_sequence<std::tuple_size<std::decay_t<Tuple>>>::value);
}

apply is a feature of one of the library TS versions. With C++17, apply_impl could call invoke, which would work for any callable.