vendredi 19 juillet 2019

c++ use std::tie to unpack return value will lead to additional cost?

Consider these two approaches

approach 1

std::tuple<TypeA, TypeB> res = function(args);
TypeA & a = std::get<0>(res);
TypeB & b = std::get<1>(res);
// use a and b as you want

approach 2

TypeA a;
TypeB b;
std::tie(a, b) = function(args);
// use a and b as you want

I'm considering what's the cons and pros of these two approaches.

Will approach 2 lead to additional cost? Or maybe they are totally equivalent?

and in c++17, the structure binding provides a more intuitively way to unpack tuple

auto [ var1, var2 ] = tuple;

Will it bring any improvement compared to another two approaches list above? Or it's just syntactic sugar?

Aucun commentaire:

Enregistrer un commentaire