内のコードに続いて、この質問、私が持っているstd::bind
可変長テンプレート関数で。auto
returnを使用して関数テンプレートを提供しようとすると、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に提出)。
解決策は...使用しないことstd::bind()
です。とにかく理由はほとんどありません。ラムダは厳密に優れています:
template <typename... Args>
auto inv(Args... args) {
return [=]{ return inv_impl(args...); };
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加