ご覧くださりありがとうございます。
threadクラス及び関数オブジェクトの学習をしております。
下記コードをコンパイルしたら一部エラーが出ました。
C++
1#include <thread> 2#include <string> 3 4using namespace std; 5 6void foo() { 7 printf("mainとは別スレッドで実行されています\n"); 8} 9 10struct bar1 { 11 void operator() (const string& msg) const { 12 printf("%sが渡されました\n",msg.c_str()); 13 } 14}; 15 16struct bar2 { 17 void operator() () { 18 printf("引数なしです\n"); 19 } 20}; 21 22int main() { 23 thread th1(foo); 24 thread th2(bar1(), "An argument"); 25 thread th3(bar2()); 26 th1.join(); 27 th2.join(); 28 th3.join(); 29 30 return 0; 31}
エラーは以下のようにth3についてです。
__
main.cpp:25:13: warning: parentheses were disambiguated as a function
declaration [-Wvexing-parse]
thread th3(bar2());
^~~~~~~~
main.cpp:25:14: note: add a pair of parentheses to declare a variable
thread th3(bar2());
^
( )
main.cpp:28:6: error: member reference base type
'std::__1::thread (bar2 (*)())' is not a structure or union
th3.join();
__
bar2のインスタンスを作ってそれをthread引数に渡せば回避できることは把握しております。
ただ、
thread th2(bar1(), "An argument");
が通るのに
thread th3(bar2());
が通らないのがなんだか釈然としません。(上記エラーの意味もわかりません)
何が起きていてのエラーなのか、もしわかればC++11の理解が進み大変助かります。
ご存知の方がいらっしゃれば是非ご教示お願いいたします
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/03/06 04:25
2017/03/06 10:29