2016-05-08 14 views

答えて

0

static_castを使用して、それらの曖昧さを排除できます。

static_castを使用すると、std :: transform(s.begin()、s.end()、s.begin()などの特定の型への関数からポインタへの変換を実行して関数のオーバーロードを曖昧さをなくすこともできます。 )、static_cast(std :: toupper));

thread t1{static_cast<void(*)(int)>(foo),9}; 
thread t2{static_cast<void(*)(string)>(foo),"nine"}; 
2

あなたが希望過負荷機能を選択するためにキャストを使用する必要がありますを取得します。私はシンプルさと読みやすさのためにラムダ関数を使用したい

void foo(int n){cout << n << '\n';} 

void foo(string s){cout << s << '\n';} 

int main(){ 
    void (*foo1)(int) = foo; 
    void (*foo2)(string) = foo; 
    thread t1(foo1,9); 
    thread t2(foo2,"nine"); 
    t1.join(); 
    t2.join(); 
    return 0; 
} 
2

:ここ

はそれを行うための作業コードである

thread t1([]{foo(9); }); 
thread t2([]{foo("str");}); 
0

それとも、C-styleが直接それを唱えてもよい。

thread t1{(void (*)(int))foo,9}; 
thread t2{(void (*)(string))foo,"nine"}; 
+0

あなたは民間ベースにキャストする必要がない限り、あなたは本当にC++コードにC++スタイルのキャストを使用して優れています。 – StoryTeller

+2

さて、 'thread t1 {static_cast (foo)、9}; t2 {static_cast (foo)、 "nine"}; 'しかし、もっと長いです。 – bipll

関連する問題