I have a simple do_something template function that returns an integer: 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; }
With GCC 6.1.1, I get this error:
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);
As you can see, the compiler cannot determine the type of the result of the function.
Please note: clang ++ 3.8.0 can compile this without any errors.
So my question is: is there a way to specify the expected return value from a template function, for example, in this case?
source share