[C ++ 11]のstd :: refの&&のstd :: reference_wrapperノート

原因はパラメータリストのスレッド目的球は、参照がある場合は、時間のSTD ::スレッドを初期化するために話を聞いた「CplusplusConcurrencyInAction_PracticalMultithreading」の時間を、見ている、あなたはSTDを渡す必要があります:: refは期待に沿った結果を得ることができます。

見つかったのstd ::をチェックrefはSTD :: reference_wrapperを生成するために使用されます。よるcppreferenceの  それの言葉

、単語を使用すること、時にはいくつかの場所では、我々は所望の基準に一致しない使用する場合がありますコピーをデフォルト(例えば、伝統的な価値観のSTLコンテナとして、また、そのようなのstd ::バインドなど)。

特に、以下のいくつかの例で参照してください:

#include <functional>
#include <iostream>
 
void f(int& n1, int& n2, const int& n3)
{
    std::cout << "In function: " << n1 << ' ' << n2 << ' ' << n3 << 'n';
    ++n1; // increments the copy of n1 stored in the function object
    ++n2; // increments the main()'s n2
    // ++n3; // compile error
}
 
int main()
{
    int n1 = 1, n2 = 2, n3 = 3;
    std::function<void()> bound_f = std::bind(f, n1, std::ref(n2), std::cref(n3));
    n1 = 10;
    n2 = 11;
    n3 = 12;
    std::cout << "Before function: " << n1 << ' ' << n2 << ' ' << n3 << 'n';
    bound_f();
    std::cout << "After function: " << n1 << ' ' << n2 << ' ' << n3 << 'n';
}

Before function: 10 11 12
In function: 1 11 12
After function: 10 12 12

私たちは期待に沿った結果の値にはstd :: refのパスを使用しながら、パラメータのダイレクトパスの値は、変更されていないN1ことがわかりました。

N1の期待値を満たしていない理由は、STDを呼び出している::バインド時に最初のパラメータをコピーし、その後、N1参照は、main関数の定義に渡されていない関数fに渡されたとき、しかし、 n1は参照を取得するために、一時的な変数にコピー。関数f n1の変形例では、N1の値は、あるコピーのみすることN1を変更することなく、その一貫性、。

ここにありますstd::reference_wrapper 的例子

#include <algorithm>
#include <list>
#include <vector>
#include <iostream>
#include <numeric>
#include <random>
#include <functional>
 
int main()
{
    std::list<int> l(10);
    std::iota(l.begin(), l.end(), -4);
 
    std::vector<std::reference_wrapper<int>> v(l.begin(), l.end());
    // 不能在 list 上用 shuffle (要求随机访问),但能在 vector 上使用它
    std::shuffle(v.begin(), v.end(), std::mt19937{std::random_device{}()});
 
    std::cout << "Contents of the list: ";
    for (int n : l) std::cout << n << ' '; std::cout << 'n';
 
    std::cout << "Contents of the list, as seen through a shuffled vector: ";
    for (int i : v) std::cout << i << ' '; std::cout << 'n';
 
    std::cout << "Doubling the values in the initial list...n 大专栏  [C++11 ] std::ref&&std::reference_wrapper  notes4;;
    for (int& i : l) {
        i *= 2;
    }
 
    std::cout << "Contents of the list, as seen through a shuffled vector: ";
    for (int i : v) std::cout << i << ' '; std::cout << 'n';
}


Contents of the list: -4 -3 -2 -1 0 1 2 3 4 5 
Contents of the list, as seen through a shuffled vector: -1 2 -2 1 5 0 3 -3 -4 4 
Doubling the values in the initial list...
Contents of the list, as seen through a shuffled vector: -2 4 -4 2 10 0 6 -6 -8 8

デフォルトのパラメータは、まだコピーである、とstd ::上バインド同様の状況、それが使用STDする必要がある場合には最後に、我々はSTD ::スレッドthread目的球に渡されたパラメータを作成することによって、最初に戻る:: REF

参考文献:

  * 《CplusplusConcurrencyInAction_PracticalMultithreading》2.2节
  * [std::ref](https://zh.cppreference.com/w/cpp/utility/functional/ref)
  * [std::reference_wrapper](https://zh.cppreference.com/w/cpp/utility/functional/reference_wrapper)
  * [C++ Difference between std::ref(T) and T&?](https://stackoverflow.com/questions/33240993/c-difference-between-stdreft-and-t)
  * 

誠実鑑賞、店でない限り

使用マイクロチャネルは、完全な支払に2次元コードをスキャン


おすすめ

転載: www.cnblogs.com/liuzhongrong/p/12371257.html