12

サードパーティ関数を介して、別のメソッドからメソッドを呼び出したい。ただし、両方とも可変個のテンプレートを使用します。例えば:

void third_party(int n, std::function<void(int)> f)
{
  f(n);
}

struct foo
{
  template <typename... Args>
  void invoke(int n, Args&&... args)
  {
    auto bound = std::bind(&foo::invoke_impl<Args...>, this,
                           std::placeholders::_1, std::forward<Args>(args)...);

    third_party(n, bound);
  }

  template <typename... Args>
  void invoke_impl(int, Args&&...)
  {
  }
};

foo f;
f.invoke(1, 2);

問題は、コンパイル エラーが発生することです。

/usr/include/c++/4.7/functional:1206:35: error: cannot bind ‘int’ lvalue to ‘int&&’

ラムダを使用してみましたが、GCC 4.8 はまだ構文を処理していない可能性があります。ここに私が試したものがあります:

auto bound = [this, &args...] (int k) { invoke_impl(k, std::foward<Args>(args)...); };

次のエラーが表示されます。

error: expected ‘,’ before ‘...’ token
error: expected identifier before ‘...’ token
error: parameter packs not expanded with ‘...’:
note:         ‘args’

私が理解していることから、コンパイラはinvoke_impltypeでインスタンス化することを望んでいますが、この場合に使用すると実際の引数の型が保持されるint&&と思いました。&&

私は何を間違っていますか?ありがとう、

4

1 に答える 1