const へのローカル左辺値参照と右辺値参照は、一時変数の有効期間を延長できます。
const std::string& a = std::string("hello");
std::string&& b = std::string("world");
初期化子が単純な式ではなく、条件演算子を使用している場合にも機能しますか?
std::string&& c = condition ? std::string("hello") : std::string("world");
結果の 1 つが一時オブジェクトで、もう 1 つがそうでない場合はどうなるでしょうか。
std::string d = "hello";
const std::string& e = condition ? d : std::string("world");
C++ は、条件が false の場合に一時的な有効期間を延長することを義務付けていますか?
コピー不可能なオブジェクトに関するこの質問に答えているときに、質問が出てきました。