Почему не может быть выведено F
для proxy()
?Не удается вывести аргумент шаблона, который является функцией
Это должно быть возможно, потому что я ограничиваю его - только для функций, возвращающих int
.
#include <utility>
#include <iostream>
#include <type_traits>
using namespace std;
int foo(int bar) {
cout << "int" << endl;
return 2;
}
float foo(float bar) {
cout << "float" << endl;
return 1;
}
template <typename F, typename... Args>
typename enable_if<
is_same<
typename result_of<F(Args...)>::type,
int
>::value,
typename result_of<F(Args...)>::type
>::type
proxy(F func, Args&&... args) {
return func(forward<Args>(args)...);
}
int main() {
proxy(foo, 5);
}
Здесь ошибка:
b.cpp:29:17: error: no matching function for call to 'proxy(<unresolved overloaded function type>, int)'
b.cpp:24:1: note: template argument deduction/substitution failed:
b.cpp:29:17: note: couldn't deduce template parameter 'F'
Поскольку две перегруженные функции ... Компилятор не может вывести значение посредствои будущий (следующий) параметр функции ...'proxy (static_cast (foo), 5);' будет делать трюк здесь –