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,或者这是未指定的?
发布于 2012-12-09 00:25:27
标准谈到了元组赋值艺术§20.4.2.2 tuple.assign,唯一提到的例外是赋值不应该抛出,除非分配给抛出的元素之一。
由于没有提到元素被分配到的顺序,因此没有指定。
https://stackoverflow.com/questions/13779051
复制相似问题