std :: bind与可变参数模板和自动返回类型 [英] std::bind with variadic template and auto return type

查看:83
本文介绍了std :: bind与可变参数模板和自动返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照这个问题中的代码,我有一个带有可变模板的 std :: bind 功能.如果我尝试提供带有 auto 返回值的功能模板,则gcc会拒绝该程序:

Following the code in this question, I have a std::bind with a variadic template function. If I try to provide a function template with auto return, gcc rejects the program:

#include <functional>

template <typename... Args
auto inv_impl(Args... a) { return (a + ...); }

template <typename... Args>
auto inv(Args... args) {
  auto bound = std::bind(&inv_impl<Args...>, args...);
  return bound;
}

int main() {
  auto b = inv(1, 2);
}

编译错误为:

foo.cc: In instantiation of ‘auto inv(Args ...) [with Args = {int, int}]’:
foo.cc:41:30:   required from here
foo.cc:36:25: error: no matching function for call to ‘bind(<unresolved overloaded function type>, int&, int& ’
   auto bound = std::bind(&inv_impl<Args...>, args...);
                ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from foo.cc:2:
/usr/include/c++/8.1.1/functional:808: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++/8.1.1/functional:808:5: note:   template argument deduction/substitution failed:
foo.cc:36:25: note:   couldn't deduce template parameter ‘_Func’
   auto bound = std::bind(&inv_impl<Args...>, args...);
                ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from foo.cc:2:
/usr/include/c++/8.1.1/functional:832: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++/8.1.1/functional:832:5: note:   template argument deduction/substitution failed:
foo.cc:36:25: note:   couldn't deduce template parameter ‘_Result’
   auto bound = std::bind(&inv_impl<Args...>, args...);
                ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
foo.cc:37:10: error: unable to deduce ‘auto’ from ‘bound’
   return bound;
          ^~~~~ 
foo.cc: In function ‘int main()’:
foo.cc:41:8: error: ‘void b’ has incomplete type
   auto b = inv<int, int>(1, 2);
        ^

据我所知,返回类型是我的工作说明的,只有 auto 返回类型,编译器无法处理.

As far as I see, return types spelled out by me work, and it's only auto return types that the compiler can't handle.

有没有一种方法可以在写代码时不知道返回类型的情况下从inv_impl返回?(我在玩 declval / decltype 构造,但是我想知道是否还有更好的东西)

Is there a way I can return from inv_impl without knowing the return type at code-write time? (I'm playing with declval/decltype constructs but I'm wondering if there's something better)

推荐答案

这绝对是gcc的错误(提交为 86826 ).

This is definitely a gcc bug (filed 86826).

解决方案是...不使用 std :: bind().无论如何,几乎没有理由.Lambda严格来说是优越的:

The solution is to just... not use std::bind(). There's hardly ever a reason to anyway. Lambdas are strictly superior:

template <typename... Args>
auto inv(Args... args) {
  return [=]{ return inv_impl(args...); };
}

这篇关于std :: bind与可变参数模板和自动返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆