std :: bindと可変個引数テンプレートおよび自動戻り値の型

pseyfert

内のコードに続いて、この質問、私が持っているstd::bind可変長テンプレート関数で。autoreturnを使用して関数テンプレートを提供しようとすると、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]

編集
0

コメントを追加

0

関連記事

分類Dev

可変個引数テンプレート、型推定、およびstd :: function

分類Dev

std :: bindおよびstd :: placeholdersを使用する可変個引数テンプレートファクトリ

分類Dev

可変個引数テンプレートを使用したstd :: ref()およびstd :: bind()について少しぼんやりしています

分類Dev

可変個引数テンプレートの拡張、継承、およびstd :: unique_ptr

分類Dev

std :: functionの可変個引数テンプレート引数

分類Dev

c ++ 11可変個引数テンプレートとstd :: endl

分類Dev

多次元std :: arrayの可変個引数テンプレート

分類Dev

constexpr可変個引数テンプレートとstd :: arrayの解凍

分類Dev

可変個引数テンプレート内のstd :: bindを使用した参照の完全な転送

分類Dev

std :: holds_alternative可変個引数テンプレート

分類Dev

可変個引数テンプレートの構築における暗黙のstd :: pairの構築

分類Dev

std :: functionalのC ++可変個引数関数テンプレート

分類Dev

可変個引数テンプレートでのstd :: placeholdersの使用

分類Dev

可変個引数テンプレートの存在に基づくstd :: enable_if

分類Dev

std :: bindおよび関数テンプレート

分類Dev

可変個引数テンプレートパラメータでstd :: bindを使用できません

分類Dev

std :: tupleに展開する可変個引数テンプレート

分類Dev

可変個引数テンプレートメソッドとstd :: function-コンパイルエラー

分類Dev

std :: functionをパラメーターとして使用する可変個引数テンプレート

分類Dev

可変個引数テンプレート関数:呼び出しに一致する関数がありません、std :: endl

分類Dev

可変個引数テンプレート内のラムダからstd :: functionへの暗黙的な変換

分類Dev

std :: functionでの可変個引数テンプレートのパラメータマッチング

分類Dev

std :: pairの区分的コンストラクターにおける可変個引数テンプレートの質問

分類Dev

std :: functionおよびstd :: bindの戻り値

分類Dev

可変個引数型リストプレフィックスのc ++ std :: tuple

分類Dev

可変個引数テンプレートを使用したstd :: functionの使用方法

分類Dev

テンプレートの可変個引数をstd :: ofstreamに格納する方法は?

分類Dev

std :: functionおよびstd :: bindでC ++テンプレートを使用する

分類Dev

戻り値の型とテンプレート化されたパラメーターが無効なstd :: function

Related 関連記事

  1. 1

    可変個引数テンプレート、型推定、およびstd :: function

  2. 2

    std :: bindおよびstd :: placeholdersを使用する可変個引数テンプレートファクトリ

  3. 3

    可変個引数テンプレートを使用したstd :: ref()およびstd :: bind()について少しぼんやりしています

  4. 4

    可変個引数テンプレートの拡張、継承、およびstd :: unique_ptr

  5. 5

    std :: functionの可変個引数テンプレート引数

  6. 6

    c ++ 11可変個引数テンプレートとstd :: endl

  7. 7

    多次元std :: arrayの可変個引数テンプレート

  8. 8

    constexpr可変個引数テンプレートとstd :: arrayの解凍

  9. 9

    可変個引数テンプレート内のstd :: bindを使用した参照の完全な転送

  10. 10

    std :: holds_alternative可変個引数テンプレート

  11. 11

    可変個引数テンプレートの構築における暗黙のstd :: pairの構築

  12. 12

    std :: functionalのC ++可変個引数関数テンプレート

  13. 13

    可変個引数テンプレートでのstd :: placeholdersの使用

  14. 14

    可変個引数テンプレートの存在に基づくstd :: enable_if

  15. 15

    std :: bindおよび関数テンプレート

  16. 16

    可変個引数テンプレートパラメータでstd :: bindを使用できません

  17. 17

    std :: tupleに展開する可変個引数テンプレート

  18. 18

    可変個引数テンプレートメソッドとstd :: function-コンパイルエラー

  19. 19

    std :: functionをパラメーターとして使用する可変個引数テンプレート

  20. 20

    可変個引数テンプレート関数:呼び出しに一致する関数がありません、std :: endl

  21. 21

    可変個引数テンプレート内のラムダからstd :: functionへの暗黙的な変換

  22. 22

    std :: functionでの可変個引数テンプレートのパラメータマッチング

  23. 23

    std :: pairの区分的コンストラクターにおける可変個引数テンプレートの質問

  24. 24

    std :: functionおよびstd :: bindの戻り値

  25. 25

    可変個引数型リストプレフィックスのc ++ std :: tuple

  26. 26

    可変個引数テンプレートを使用したstd :: functionの使用方法

  27. 27

    テンプレートの可変個引数をstd :: ofstreamに格納する方法は?

  28. 28

    std :: functionおよびstd :: bindでC ++テンプレートを使用する

  29. 29

    戻り値の型とテンプレート化されたパラメーターが無効なstd :: function

ホットタグ

アーカイブ