Template type deduction with std::function

后端 未结 2 1644
谎友^
谎友^ 2020-12-10 05:39

I have discovered the following behaviour with std::function and type deduction, which was unexpected for me:

#include 

templ         


        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-10 06:07

    No implicit conversion is performed during template argument deduction, except: temp.deduct.call

    In general, the deduction process attempts to find template argument values that will make the deduced A identical to A (after the type A is transformed as described above). However, there are three cases that allow a difference:

    • If the original P is a reference type, the deduced A (i.e., the type referred to by the reference) can be more cv-qualified than the transformed A.
    • The transformed A can be another pointer or pointer to member type that can be converted to the deduced A via a function pointer conversion ([conv.fctptr]) and/or qualification conversion ([conv.qual]).
    • If P is a class and P has the form simple-template-id, then the transformed A can be a derived class of the deduced A. Likewise, if P is a pointer to a class of the form simple-template-id, the transformed A can be a pointer to a derived class pointed to by the deduced A.

    However, if the template parameter doesn't participate in template argument deduction, implicit conversion will be performed: (temp.arg.explicit)

    Implicit conversions (Clause [conv]) will be performed on a function argument to convert it to the type of the corresponding function parameter if the parameter type contains no template-parameters that participate in template argument deduction. [ Note: Template parameters do not participate in template argument deduction if they are explicitly specified.

    So, if you explicitly specify the template argument, it should work:

    stdfunc_test([](int _) {return _ + 2;});
    stdfunc_test(test_func);
    

提交回复
热议问题