Is using `std::get` on a `std::tuple` guaranteed to be thread-safe for different values of `I`?

Since std::get has no explicit statements in the specification about its data race properties, we fall back to the default behavior defined in [res.on.data.races]. Specifically, paragraphs 2 and 3 tell the story: A C++ standard library function shall not directly or indirectly access objects (1.10) accessible by threads other than the current thread unless the … Read more

Why does std::tuple break small-size struct calling convention optimization in C++?

It seems to be a matter of ABI. For instance, the Itanium C++ ABI reads: If the parameter type is non-trivial for the purposes of calls, the caller must allocate space for a temporary and pass that temporary by reference. And, further: A type is considered non-trivial for the purposes of calls if it has … Read more

What’s the purpose of const swap() function?

This was introduced in the “zip” proposal P2321 originally described in “A Plan for C++23 Ranges” P2214. P2321 swap for const tuple and const pair. Once tuples of references are made const-assignable, the default std::swap can be called for const tuples of references. However, that triple-move swap does the wrong thing: int i = 1, … Read more

Using tuple in unordered_map

The template arguments for an unordered_map looks like this: template< class Key, class T, class Hash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class Allocator = std::allocator< std::pair<const Key, T> > > class unordered_map; std::hash is not specialized for tuples (scroll down to Standard specializations for library types). Therefore you need to provide your own, something … Read more

What is the reason for `std::make_tuple`?

Because you cannot use argument deduction for constructors. You need to write explicitly std::tuple<int, double>(i,d);. It makes it more convenient for creating a tuple and passing it to another function in one-shot. takes_tuple(make_tuple(i,d)) vs takes_tuple(tuple<int,double>(i,d)). One less place to change when the type of i or d changes, especially if there were possible conversions to … Read more

C++ std::tuple order of destruction

I’ll offer a life lesson I’ve learned, rather than a direct answer, in response to your question: If you can formulate, for multiple alternatives, a reasonable argument for why that alternative should be the one mandated by the standard – then you should not assume any of them is mandated (even if one of them … Read more

Difference between std::pair and std::tuple with only two members?

There are some differences: std::tuple is not required by the standard to ever be standard-layout. Every std::pair<T, Y> is standard-layout if both T and Y are standard-layout. It’s a bit easier to get the contents of a pair than a tuple. You have to use a function call in the tuple case, while the pair … Read more