[英]How exception-safe is std::tie?
std::tie
返回一個引用元組,因此您可以執行以下操作:
int foo, bar, baz;
std::tie(foo, bar, baz) = std::make_tuple(1, 2, 3);
這類似於Python中的foo, bar, baz = (1, 2, 3)
。
如果其中一個分配拋出,應該發生什么,如下例所示?
int foo = 1337;
struct Bar {
Bar& operator=(Bar) { throw std::exception{}; }
} bar;
try {
std::tie(foo, bar) = std::make_tuple(42, Bar{});
} catch (std::exception const&) {
std::cout << foo << '\n';
}
它會打印1337或42,還是未指定?
標准提到元組賦值藝術§20.4.2.2[tuple.assign] ,唯一提到的例外是賦值不應拋出,除非其中一個元素分配給throws。
由於沒有提及分配元素的順序,因此未指定 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.