Questions tagged [stdtuple]

Use this tag for questions about the C++ standard library template std::tuple. It represents an ordered, heterogeneous sequence of objects. Also add the tag [c++] when using this tag.

Also see and .

403 questions
25
votes
2 answers

How to get reference to an element of a std::tuple?

You can get the value of the nth element of an std::tuple using std::get(tuple). But I need to pass one element of that tuple as reference to a function. How do I get the reference to an element of a std::tuple?
danijar
  • 32,406
  • 45
  • 166
  • 297
24
votes
2 answers

How to get N-th type from a tuple?

I want to make a template where I can input an index and it will give me the type at that index. I know I can do this with decltype(std::get(tup)) but I would like to implement this myself. For example, I would like to do this, typename get
user2030677
  • 3,448
  • 4
  • 23
  • 36
23
votes
3 answers

Why is it not good to use recursive inheritance for std::tuple implementations?

In this question, Howard Hinnant said Some implementations of std::tuple use recursive inheritance. But the good ones don't. ;-) Can someone please shed some light on that?
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
22
votes
1 answer

Is returning a 2-tuple less efficient than std::pair?

Consider this code: #include #include std::pair f1() { return std::make_pair(0x111, 0x222); } std::tuple f2() { return std::make_tuple(0x111, 0x222); } Clang 3 and 4 generate similar code for both on…
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
22
votes
4 answers

Structured binding to replace std::tie abuse

In reading this summary of the c++17 final features I was a bit surprised by the section on structured bindings (emphasis mine): structured bindings Until now, there was a known trick to abuse std::tie to assign a tuple or pair to different…
Emerald Weapon
  • 2,392
  • 18
  • 29
20
votes
4 answers

Return value optimization of tuple/tie

I am looking into return value optimization in the case of tuple/ties and the behavior I observe is not as I expected. In the example below I would expect move semantics to kick in, which it does, but there is one copy operation which remains. The…
thorsan
  • 1,034
  • 8
  • 19
18
votes
3 answers

Confused by default constructor description of std::tuple in the ISO C++ Standard

The Standard says that std::tuple has the following member functions constexpr tuple(); explicit tuple(const Types&...); Can someone please explain what is supposed to happen for std::tuple<>?
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
17
votes
4 answers

Making `std::get` play nice with SFINAE

std::get does not seem to be SFINAE-friendly, as shown by the following test case: template auto foo(C &c) -> decltype(std::get(c)) { return std::get(c); } template void foo(...) { } int main() { …
Quentin
  • 62,093
  • 7
  • 131
  • 191
16
votes
3 answers

Why would you explicitly move a forwarding reference?

I'm looking at some code, and I see the following function: template static return_t make_return(Args &&... args) { // using std::forward will preserve lvalue args as such, but the point of this function // is to…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
16
votes
2 answers

Is a tuple guaranteed to compress empty elements?

The implementations of std::tuple in both libstdc++ and libc++ use (I presume) the empty base class optimisation to compress empty elements: struct empty {}; static_assert(sizeof(std::tuple) == sizeof(int), ""); // (1) My question is…
Tristan Brindle
  • 16,281
  • 4
  • 39
  • 82
15
votes
2 answers

Why does overloading operator< for std::tuple not seem to work in priority_queue?

Here is the MWE: #include #include #include using namespace std; bool operator<(tuple lhs, tuple rhs) { return get<1>(lhs) < get<1>(rhs); } int main() { priority_queue
MKCCT
  • 177
  • 6
15
votes
2 answers

Structured bindings for your own type that isn’t a struct or a tuple(via public member function)

I am going through the Herb Sutter's A journey: Toward more powerful and simpler C++ programming Structure Binding section In order to understand the concept .Best is to write a program I tried but getting some error Just want to try how to use…
Hariom Singh
  • 3,512
  • 6
  • 28
  • 52
15
votes
6 answers

Produce std::tuple of same type in compile time given its length by a template argument

In c++, how can I implement a function with an int template argument indicating the tuple length and produce a std::tuple with that length? E.g. func<2>() returns std::tuple(); func<5>() returns std::tuple().
yuefengz
  • 3,338
  • 1
  • 17
  • 24
15
votes
2 answers

Requirements for std::ignore

C++11 introduces an object called std::ignore: const /* unspecified */ ignore; For brevity, let typedef decltype(std::ignore) T; From what I can tell, the only requirement for T is that it is CopyAssignable, due to the specification of std::tie…
nknight
  • 1,034
  • 8
  • 17
1
2
3
26 27