繁体   English   中英

如何使用 lambda 在 std::function 参数中推导出模板类型?

[英]How to have template type deduced in std::function arguments with lambda?

我有一个 boost::variant 并且我只想在变体是特殊类型时才执行函子,所以我编写了这个函数:

template<typename T, typename Variant>
void if_init(Variant& opt_variant, std::function<void(T)> functor){
    if(auto* ptr = boost::get<T>(&opt_variant)){
        functor(*ptr);
    }   
}

这很好用,但我希望推导出类型 T,以便我可以这样写:

if_init(b, [](double var){ std::cout << "I'm double and set" << std::endl; });

但是没有推断出类型:

type_inference.cpp:19:5: error: no matching function for call to 'if_init'
    if_init(b, [](double var){ std::cout << "I'm double and set" << std::endl; }); 
    ^~~~~~~
type_inference.cpp:10:6: note: candidate template ignored: failed template argument deduction
    void if_init(Variant& opt_variant, std::function<void(T)> functor){

如果我写:

if_init<double>(b, [](double var){ std::cout << "I'm double and set" << std::endl; });

它运作良好。

有没有办法推导出类型 T ? 我只想输入 T 一次。 这里的类型很短,但在实际情况下,有长类型。

我正在使用 CLang 3.2。

这是完整的测试用例(第一个调用编译不是第二个):

#include <iostream>
#include <functional>
#include <boost/variant.hpp>

typedef boost::variant<int, double> Test;

template<typename T, typename Variant>
void if_init(Variant& opt_variant, std::function<void(T)> functor){
    if(auto* ptr = boost::get<T>(&opt_variant)){
        functor(*ptr);
    }   
}

int main(){
    Test b = 1.44; 

    if_init<double>(b, [](double var){ std::cout << "I'm double and set" << std::endl; });
    if_init(b, [](int var){ std::cout << "I'm int and set" << std::endl; });      

    return 0;
}

我建议您将std::function<Sig>视为符合Sig作为签名的任何一个函子的容器 - 并且可以随时替换。 这个功能对例如std::vector<std::function<Sig>>非常方便,因为这样的容器可以容纳不同类型的函子。

在您的情况下,因为您只关心一个函子,所以您真的不需要std::function<Sig>的功能。 因此,我建议您像这样声明您的函数模板:

template<typename T, typename Variant, typename Functor>
void if_init(Variant& opt_variant, Functor functor);

如果您担心这不能说明Functor必须符合void(T)签名,请注意std::function<Sig>也不强制执行此操作:尽管显然您最终遇到编译错误,但它不是一个很好的。 计划进行更改(也许您的实现也有),但更改为另一种错误。 对您的情况仍然没有帮助。

我个人使用模板别名( 在模板参数列表中)来记录和强制执行函子应符合的内容。 这最终看起来像:

// Documents that e.g. long l = std::forward<Functor>(functor)(42.)
// should be a valid expression -- a functor that returns int would
// also be accepted.
// Triggers a hard-error (typically a static_assert with a nice message)
// on violation.
template<typename Functor, Requires<is_callable<Functor, long(double)>>...>
R foo(Functor functor);

// Documents that this function template only participates in overload resolution
// if the functor conforms to the signature.
// Does not trigger a hard-error (necessary by design); if everything goes right
// then another overload should be picked up -- otherwise an error of the kind
// 'no matching overload found' is produced
template<typename Functor, EnableIf<is_callable<Functor, long(double)>>...>
R bar(Functor functor);

至于您的确切问题,C++ 的规则不允许在您的情况下推导出模板参数。 这真的不是一个容易解决的“问题”,如果它是一个。 您可以找到有关此的更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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