このtoyの例を実行しようとすると、矛盾するパラメータパックコンパイラエラーが発生します。誰かがなぜint 'がintとして推定されているのかを明らかにしてもらえますか&ここですか?以下の例では、以下の 'test'関数をintリテラルで実行するとうまく動作します。説明のために事前に感謝!メンバ関数を実行するスレッドを作成するvariadicテンプレートメンバ関数のintとintの不一致
class Test {
public:
Test() {}
~Test() {
t.join();
}
void print(int num)
{
std::cout << num << std::endl;
}
template<class ...Args>
void test(void(Test::*b)(Args...) , Args&&... args)
{
t = std::thread(b, this, std::forward<Args>(args)...);
}
std::thread t;
};
int main()
{
int a = 123;
Test test;
test.test(&Test::print, a);
// test.test(&Test::print, 123); works
}
エラー:
prog.cc: In function 'int main()':
prog.cc:82:40: error: no matching function for call to 'Test::test(
void (Test::*)(int), int&)'
test.test(&Test::print, a);
^
prog.cc:82:40: note: candidate is:
prog.cc:62:10: note: template<class ... Args> void Test::test(void
(Test::*)(Args ...), Args&& ...)
void test(void(Test::*b)(Args...) , Args&&... args)
^
prog.cc:62:10: note: template argument deduction/substitution failed:
prog.cc:82:40: note: inconsistent parameter pack deduction with 'int' and
'int&'
test.test(&Test::print, a);
^
'printThree'とは何ですか? – Sergey
@ Sergey typo-fixed。ごめんなさい。 – Mozbi
あなたは完璧な転送を使用しています。その全体のポイントは、lvalue引数のlvalue参照型を推定する引数の値カテゴリ、rvalue引数のrvalue参照型を保持することです。それは仕事をしているように見える、まあ、完璧。 –