c++ - GCC 无法从模板函数中推断出自动返回类型?

标签 c++ templates gcc type-deduction

我有一个简单的模板函数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 可以编译无任何错误。

所以我的问题是:有没有办法像这种情况一样指定模板函数的预期返回值?

最佳答案

看起来编译器不确定 do_something<int> 的类型- 我不确定这是编译器问题还是语言问题 - 但您可以强制编译器通过使用 do_something<int> 来整理其类型之前以一种相对琐碎的方式。例如,以下代码在 gcc 和 clang trunk 版本下编译正常(根据 godbolt)。

#include <functional>

template<typename T>
auto do_something(T input) {
  return 123;
}

// Make the compiler workout the type of do_something<int> so we can use it later.
auto f = do_something<int>;

int main(int argc, char *argv[]) {
  std::function<int(void)> function = std::bind(do_something<int>, 12);
  function();
  return 0;
}

关于c++ - GCC 无法从模板函数中推断出自动返回类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38175427/

相关文章:

c++ - 对象管理器的最佳实践

c++ - 将 unsigned char *buf=NULL 翻译成 Pascal?

c++ - 不匹配 operator= 错误

c++ - 函数模板和 type_traits 的问题

javascript - Node.js - 将平面 json 转换为不带 'parent' ,'child' 属性的分层 json

c++ - Code::Blocks 和 boost 1.55:存在动态库时不使用静态库

c++ - 编写预处理器函数 : Is the syntax correct

c++ - 在 C++ 中从字符串创建类以避免大的 switch 语句?

c++ - 告诉 linux 二进制文件在哪里加载共享库

java - 从方法返回 "void"或 "null"