如何声明没有std::function的函数返回函数

C++ How to declare function-returning function without std::function?

本文关键字:函数 std function 返回 何声明 声明      更新时间:2023-10-16

尝试一些构图的想法,我有

#include <iostream>
#include <functional>
using namespace std;
struct A { };
struct B { };
struct C { };
B b4a (A a) {   B b; return b;   }
C c4b (B b) {   C c; return c;   }
template <typename R, typename S, typename T>
function<R(T)> composition
(   R(r4s)(S)
,   S(s4t)(T)   ) 
{   return [=] (T t) {   return r4s (s4t (t));  };  }
int main()
{
    cout << "b4a : " << type_name<decltype(b4a)>() << endl;
    cout << "c4b : " << type_name<decltype(c4b)>() << endl;
    auto c4a = composition<C, B, A> (c4b, b4a);
    cout << "c4a : " << type_name<decltype(c4a)>() << endl;
    auto lb4a = [=] (A a) { B b; return b; };
    auto lc4b = [=] (B b) { C c; return c; };
    auto lc4a = composition<C, B, A>(lc4b, lb4a);
    cout << "lc4a : " << type_name<decltype(lc4a)>() << endl;
}

(type_name的定义有点偏离主题,但是您可以在这里看到上面的完整工作示例)。

可以看到,composition操作符既适用于函数指针(如b4a),也适用于lambda指针(如lb4a)。我想摆脱compositionfunction<R(T)>返回类型,只是直接声明它的类型,但我不知道如何以任何其他方式表示它的返回类型。尝试

R(composition)(T)

R(*composition)(T)

R(&composition)(T)

产生'composition' declared as function returning a function,让我怀疑这是不可能的。线索吗?

lambda具有唯一但未指定的类型。一种可能的解决方法是不使用lambdas,而是创建自己的等效类:

template <typename R, typename S, typename T>
struct Composition {
    R (*r4s)(S);
    S (*s4t)(T);
    R operator()(T t) const { return r4s(s4t(t)); }
};
template <typename R, typename S, typename T>
Composition<R,S,T> composition(R (*r4s)(S),S(*s4t)(T)) 
{   
    return {r4s,s4t};
}

不可能在运行时创建新函数。创建lambda只是创建lambda闭包类型的一个实例(它是一个类),但是函数本身是在编译时作为类的operator()创建的。

lambda表达式捕获的函数指针r4ss4t成为闭包对象的成员。如果您真的可以创建r4ss4t的组合作为一个新函数,那么可以说,它们将"嵌入到代码中"。这类事情在c++中是不可能完成的。

您应该像这里一样使用std::function

@Vaughn Cato的回答(现场演示)的一个通用的,可变的变体:

template <typename...>
class Composition {
    // This "base case" is only used for zero-function compositions.
public:
    void operator () () {}
};
template <typename F, typename... Remainder>
class Composition<F, Remainder...> : private Composition<Remainder...> {
    // This specialization matches all non-zero function compositions.
    using base_t = Composition<Remainder...>;
    F f_;
    // Use tag dispatching to determine if F is the last function in
    // the composition, and should be called with the args, or
    // if there are further functions and f should be called with
    // the result of passing the args on to the other functions.
    using is_last = std::integral_constant<bool, sizeof...(Remainder) == 0>;
    template <typename... Args>
    auto dispatch(std::true_type, Args&&... args) ->
      decltype(f_(std::forward<Args>(args)...)) {
        return f_(std::forward<Args>(args)...);
    }
    template <typename... Args>
    auto dispatch(std::false_type, Args&&... args) ->
      decltype(f_(std::declval<base_t&>()(std::forward<Args>(args)...))) {
        return f_(static_cast<base_t&>(*this)(std::forward<Args>(args)...));
    }
public:
    template <typename T, typename... Args>
    Composition(T&& t, Args&&... args) :
        base_t(std::forward<Args>(args)...),
        f_(std::forward<T>(t)) {}
    template <typename... Args>
    auto operator () (Args&&... args) ->
      decltype(std::declval<Composition&>().dispatch(is_last{}, std::forward<Args>(args)...)) {
        return dispatch(is_last{}, std::forward<Args>(args)...);
    }
};
template <typename... Functions>
inline Composition<typename std::decay<Functions>::type...>
composition(Functions&&... f)
{   
    return {std::forward<Functions>(f)...};
}
相关文章: