У меня есть простая шаблонная функция do_something, которая возвращает целое число: 123.
template<typename T>
auto do_something(T input) {
std::this_thread::sleep_for(std::chrono::seconds(1));
return 123;
}
int main(int argc, char *argv[]) {
std::function<int(void)> function = std::bind(do_something<int>, 12);
function();
return 0;
}
С GCC 6.1.1 я получаю эту ошибку:
test.cpp: In function ‘int main(int, char**)’:
test.cpp:16:70: error: no matching function for call to ‘bind(<unresolved overloaded function type>, int)’
std::function<int(void)> function = std::bind(do_something<int>, 12);
^
In file included from /usr/include/c++/6.1.1/thread:39:0,
from test.cpp:4:
/usr/include/c++/6.1.1/functional:1331:5: note: candidate: template<class _Func, class ... _BoundArgs> typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...)
bind(_Func&& __f, _BoundArgs&&... __args)
^~~~
/usr/include/c++/6.1.1/functional:1331:5: note: template argument deduction/substitution failed:
test.cpp:16:70: note: couldn't deduce template parameter ‘_Func’
std::function<int(void)> function = std::bind(do_something<int>, 12);
^
In file included from /usr/include/c++/6.1.1/thread:39:0,
from test.cpp:4:
/usr/include/c++/6.1.1/functional:1359:5: note: candidate: template<class _Result, class _Func, class ... _BoundArgs> typename std::_Bindres_helper<_Result, _Func, _BoundArgs>::type std::bind(_Func&&, _BoundArgs&& ...)
bind(_Func&& __f, _BoundArgs&&... __args)
^~~~
/usr/include/c++/6.1.1/functional:1359:5: note: template argument deduction/substitution failed:
test.cpp:16:70: note: couldn't deduce template parameter ‘_Result’
std::function<int(void)> function = std::bind(do_something<int>, 12);
Как видите, компилятор не может вывести тип результата функции.
Обратите внимание, что clang++ 3.8.0 может скомпилировать это без каких-либо ошибок.
Итак, мой вопрос: есть ли способ указать ожидаемое возвращаемое значение из функции шаблона, как в этом случае?
-std=c++14
? - person Galik   schedule 04.07.2016auto f=do_something<int>; std::function<int(void)> function = std::bind(f, 12);
. Тем не менее, у меня недостаточноlanguage-lawyer
-fu, чтобы авторитетно заявить, является ли это ошибкой gcc или тем, как эти домино падают, относительно спецификации C++. - person Sam Varshavchik   schedule 04.07.2016std::function<int(void)> function = [](){ return do_something(42); };
вызывает создание экземпляра и решает проблему. В ожидании языкового юриста... - person skypjack   schedule 04.07.2016