首页 > 解决方案 > 为什么我不能推断出可变 lambda 的函数签名?

问题描述

我有以下实现 memoize 功能的代码。

注意问题不是关于如何专门编写一个 memoize 函数,而是关于我在这个实现中得到的编译错误以及让它工作的最小更改。

实施。

#include <functional>
#include <map>
#include <functional>
#include <iostream>

using namespace std;

template<typename T>
struct memfun_type
{
    using type = void;
};

template<typename Ret, typename Class, typename... Args>
struct memfun_type<Ret(Class::*)(Args...) const>
{
    using type = std::function<Ret(Args...)>;
};

template<typename F>
typename memfun_type<decltype(&F::operator())>::type
FFL(F const &func)
{ // Function from lambda !
    return func;
}

template <typename ReturnType, typename... Args>
std::function<ReturnType (Args...)>
memoizeImp(std::function<ReturnType (Args...)> func)
{
    std::map<std::tuple<Args...>, ReturnType> cache;
    return ([=](Args... args) mutable {
            std::tuple<Args...> t(args...);
            if (cache.find(t) == cache.end())                
                cache[t] = func(args...);
            return cache[t];
    });
}

template <typename Fn>
auto memoize(Fn && fn){
    return memoizeImp(FFL(fn));
}

和测试程序


int main()
{
    auto a = 2.;
    auto foo = [a](double x){return x+a;};
    auto foom = memoize(foo);

    std::cout << foo(1) << std::endl;
    std::cout << foom(1) << std::endl;
}

预期的输出是

3
3

但是,如果我对测试程序进行小改动

auto foo = [a](double x){return x+a;};

auto foo = [a](double x)mutable{return x+a;};

我在 gcc 上收到以下编译错误

Could not execute the program
Compiler returned: 1
Compiler stderr
<source>: In instantiation of 'auto memoize(Fn&&) [with Fn = main()::<lambda(double)>&]':
<source>:49:24:   required from here
<source>:42:26: error: invalid use of void expression
   42 |     return memoizeImp(FFL(fn));
      |                       ~~~^~~~
<source>: In instantiation of 'typename memfun_type<decltype (& F::operator())>::type FFL(const F&) [with F = main()::<lambda(double)>; typename memfun_type<decltype (& F::operator())>::type = void; decltype (& F::operator()) = double (main()::<lambda(double)>::*)(double)]':
<source>:42:26:   required from 'auto memoize(Fn&&) [with Fn = main()::<lambda(double)>&]'
<source>:49:24:   required from here
<source>:24:12: error: return-statement with a value, in function returning 'memfun_type<double (main()::<lambda(double)>::*)(double)>::type' {aka 'void'} [-fpermissive]
   24 |     return func;
      |            ^~~~

失败代码和编译错误可以在https://godbolt.org/z/74PKWvqr4查看和测试

我不确定如何修复它以使其与可变 lambda 一起使用。

标签: c++lambdac++14variadic-templatesmutable

解决方案


你缺乏专业。

添加它使其工作

template<typename Ret, typename Class, typename... Args>
struct memfun_type<Ret(Class::*)(Args...)>
{
    using type = std::function<Ret(Args...)>;
};

如果未声明 lambda,则lambda的operator()闭包类型是合格的constmutable


推荐阅读