この場合、どうなりますか?'const auto&'に値を代入するとどうなります
// assume WeakPtr is valid and has not expired
const auto& something = WeakPtr.lock();
something->doStuff();
これは未定義ですか?
このシナリオでは変更されますか?
std::shared_ptr<Something> getSomething() { return mSomething.lock(); }
const auto& something = getSomething();
これはどうですか?これらのケースのそれぞれにおいて
std::vector<int> getInts() { return std::vector<int>{ 1, 2, 3 }; }
const auto& ints = getInts();
const auto&
は、私はオブジェクトへの参照をバインドすることを意味しますが、これらの例それぞれに私は一時的な右辺値オブジェクトにバインドしています。私は災害を招いていますか?
ニートどのようにこのような質問をたくさんアップ:http://stackoverflow.com/questions/42868517/why-can-you-intialize-a-const-reference-but-not-a-non-const-reference -from-an-rv何かと便利な解説。 – user4581301
「一時的」と「rvalue」の違いに注意することが重要です。後者は厳密に一般的です。たとえば、 'const int&r = 5;'と 'const int&q = std :: move(5);'を比較します。両方とも価値観であり、1つは災害です。 –