std :: function模板参数解析 [英] std::function template argument resolution

查看:358
本文介绍了std :: function模板参数解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用库链接函数对象。

I am currently working on a library where I am chaining function objects.

我正在创建一个函数模板,该模板接受一个可调用对象(std :: function力矩),并在函数的输出和输入类型上进行参数设置。这是我正在定义的简化版本:

I am creating a function template that takes a callable object (std::function at the moment) and is parametrized on the output and input type of the function. Here is a simplified version of what I am defining:

template <typename In, typename Out>
std::vector<Out> process(std::vector<In> vals, std::function< Out(In) > func)
{
    // apply func for each value in vals
    return result;
}

我遇到的问题是使用率。似乎当我传递一个lambda时,编译器无法正确推断类型,因此抱怨该函数不存在:

The problem I am having is on usage. It seems that when I pass a lambda, the compiler cannot deduce the type correctly, so complains that the function doesn't exist:

std::vector<string> strings;
// does NOT compile
auto chars = process(strings,
        []( std::string s ) -> char
        {
            return s[0]; // return first char
        }
);

如果我将lambda明确地包装在 std :: function ,程序将编译:

If I explicitly wrap the lambda in std::function, the program compiles:

std::vector<string> strings;
// DOES compile
auto chars = process(strings,
        std::function< char(std::string) >(
        []( std::string s ) -> char
        {
            return s[0]; // return first char
        })
);

我还没有测试传递函数指针或函数对象,但是似乎很难如果我不直接传递显式的<$ c $,则编译器可以推断出 In Out 参数c> std :: function 对象。

I haven't tested passing function pointers or function objects yet, but it seems like it will be difficult for the compiler to deduce the the In and Out parameters if I'm not directly passing the explicit std::function object.

我的问题是:有没有办法解决这个问题,以便我可以推断出

My question is this: is there a way to get around this, so that I can deduce the input/return type of a callable object without explicitly mentioning them at the call site?

也许参数化了模板上的函数类型而不是输入/返回类型?基本上,我需要推断任意可调用对象的 In Out 类型。对于模板函数的返回类型,也许使用某种 auto / decltype 技巧?

Perhaps parametrize the template on the function type instead of the input/return types? Essentially I need to deduce the In and Out type for an arbitrary callable. Perhaps some kind of auto/decltype trick for the return type of the template function?

谢谢。

推荐答案

我认为您可以做的是创建中间返回类型推论使用 decltype 确定要传递给实际函数对象的参数的函数:

I think what you can do is to create an intermediate return type deducing function which uses decltype to determine the arguments to be passed to the actual function object:

template <typename Out, typename In>
std::vector<Out> process_intern(std::vector<In> vals, std::function< Out(In) > func)
{
    // whatever
}

template <typename In, typename Func>
auto process(std::vector<In> vals, Func func) -> std::vector<decltype(func(vals[0]))>
{
    return process_intern<decltype(func(vals[0]))>(vals, func);
}

当然,您可能需要考虑在<$ c $中实现逻辑否则,直接c> process(),除非有理由键入擦除函数类型。

Of course, you may want to consider implementing the logic in process() directly anyway unless there is a reason to type erase the function type.

这篇关于std :: function模板参数解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆