Does the type of order have any effects in the std :: tuple arguments?

Let's say I want to store three types in tuple : int , float and std::vector<double>

If I leave aside the questions of the subsequent interface, does this

 tuple<int, float, vector<int>> t; 

have any differences from this

 tuple<vector<int>, int, float> t; 

Due to the implementation of tuple as a class of variational bases, I expect a different layout for the created classes, but does it matter anyway ? Also, are there any optimization considerations that should be considered when placing types in a tuple (for example, put the largest first, etc.)?

+11
c ++ c ++ 11 tuples
May 28 '14 at 11:17
source share
3 answers

The standard does not impose any restrictions on the actual layout of types. The only things that affect order are the results of std::get<N> , std::tuple_element<N, T> , etc.

I know that libstdC ++ and Visual C ++ expose types in the reverse order of a given order; libC ++ sets out the types in this order. This essentially means that there is no portable way of choosing the order that always creates the best layout.

Other orders are possible. The implementation is allowed to implement a tuple with a layout that always creates a minimum size , but retains the same semantics for std::get<N> and so on. However, I do not know any standard library implementation that does this.

+15
May 28 '14 at 11:34
source share

The standard does not specify an implementation for std::tuple . However, it guarantees that std::tuple<A,B,C> will be of a different type than, for example, std::tuple<B,A,C> . std::tuple is an ordered list of types.

boost::fusion provides a data type for a container of a given type style, for cases where the order is not important: boost::fusion::set<>

+1
May 28 '14 at 12:08
source share

The standard does not specify how to implement tuple , and it is possible that the implementation reorders the arguments to create a better layout, while preserving the semantics of std::get<int N> . However, I don't know of any implementation that actually does this, since the complexity of the type set in the order that creates a good layout is complex.

It is likely that a different order of arguments will produce a different layout. If it depends on your use case. If you want to optimize here, you should consider the size limits (optional addition) and cache line size of your target architecture.

0
May 28 '14 at 11:34
source share



All Articles