std::绑定variadic模板和自动返回类型
std::bind with variadic template and auto return type
根据这个问题中的代码,我有一个带有可变模板函数的std::bind
。如果我试图提供一个返回auto
的函数模板,gcc会拒绝程序:
#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
返回类型是编译器无法处理的。
有没有一种方法可以在代码写入时从inv_impl返回而不知道返回类型?(我在玩declval
/decltype
结构,但我想知道是否有更好的东西(
这绝对是一个gcc错误(文件86826(。
解决方案是。。。不使用CCD_ 6。无论如何,几乎没有理由这么做。Lambdas绝对优越:
template <typename... Args>
auto inv(Args... args) {
return [=]{ return inv_impl(args...); };
}
相关文章:
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 运行时错误:引用绑定到类型为"int"的空指针
- 了解C++如何返回引用并绑定到引用
- 无法将运行时类绑定到 XAML T 必须是 WinRT 类型
- 为什么结构化绑定不使用"auto&"返回对结构成员的引用,而是返回成员本身
- 创建一个函数的 Python 绑定,返回指向带有 boost 的向量的指针
- 运行时错误:引用绑定到类型"int"的未对齐地址0xbebebebebebebec6,这需要 4 个字节对齐 (stl_vector.h)
- 无法将类型"T&"的非常量左值引用绑定到类型"T"的右值 t++ std::atomic<T>
- 结构化绑定初始值设定项表单 { 赋值表达式 } 对于 clang 上的数组类型失败
- 在其他容器中使用 boost::container::static_vector 时,GCC 编译错误"将'const s'绑定到类型's&'的引用丢弃限定符"
- std::绑定variadic模板和自动返回类型
- C++ 如何将参数绑定到具有相同返回类型的函数,并将它们全部存储在一个容器中
- 助推.绑定返回类型
- 通过将返回值绑定到const引用,C++性能得到改善
- 类型绑定引用中的限定符和 const 类型的初始值设定项中删除
- 提升 Python:使用 return_internal_reference 将参数的生存期与返回值绑定
- C++绑定返回类型模板参数
- Boost绑定返回类型差异
- 关于模板类的未绑定模板友元函数的返回类型
- 为什么我不能让 std::function<std::vector<T>::iterator> 绑定到返回该类型的 lambda?