2016-08-26 19 views
6

odkryłem następujące zachowanie z std::function i typu odliczenia, co było nieoczekiwane dla mnie:Szablon odliczenie z std :: funkcja

#include <functional> 

template <typename T> 
void stdfunc_test(std::function<T(T)> func) {}; 

int test_func(int arg) 
{ 
    return arg + 2; 
} 

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

Obie linie w main w wyniku błędu:

no instance of function template "stdfunc_test" matches the argument list

Podczas próby kompilacji w programie Visual Studio 2015.

Dlaczego typ dedukcji nie odejmuje typu szablonu od typu funkcji, oraz czy istnieje obejście tego problemu?

+2

Ani lambda ani wskaźnik funkcja jest faktycznie 'std :: zakresie funkcji - są domyślnie wymienialne na 'std :: function'. – aschepler

+0

Czy niejawne konwersje, takie jak te, nie są brane pod uwagę podczas określania zmiennych szablonu? – redspah

+2

Po prostu przyjmijmy, że jest to funkcja: 'template void stdfunc_test (T func)'. Następnie ogranicz zakres szablonu za pomocą SFINAE. – rustyx

Odpowiedz

1

Nie niejawna konwersja jest wykonywana podczas szablonu odliczenia argumentów, z wyjątkiem: 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.

Jednak jeśli parametr szablonu nie uczestniczy w szablonie odliczenia argumentów, niejawna konwersja zostanie przeprowadzone: (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.

Tak więc, jeśli wyraźnie określić argument szablonu, to powinno działać:

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

Można używać szablonów wydedukować podpis funkcji i funktorów:

#include<functional> 

template<class T> 
struct AsFunction 
    : public AsFunction<decltype(&T::operator())> 
{}; 

template<class ReturnType, class... Args> 
struct AsFunction<ReturnType(Args...)> { 
    using type = std::function<ReturnType(Args...)>; 
}; 

template<class ReturnType, class... Args> 
struct AsFunction<ReturnType(*)(Args...)> { 
    using type = std::function<ReturnType(Args...)>; 
}; 


template<class Class, class ReturnType, class... Args> 
struct AsFunction<ReturnType(Class::*)(Args...) const> { 
    using type = std::function<ReturnType(Args...)>; 
}; 

template<class F> 
auto toFunction(F f) -> typename AsFunction<F>::type { 
    return {f}; 
} 

template <typename T> 
void stdfunc_test(std::function<T(T)> func) {}; 

int test_func(int arg) 
{ 
    return arg + 2; 
} 


int main() 
{ 

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

można spróbować żyć tutaj: http://fiddle.jyt.io/github/d4ab355eb2ab7fc4cc0a48da261f0127