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
14
votes
1 answer

Why does the standard allow a tuple of rvalue references to be assigned to by a tuple of lvalue references?

It seems like a std::tuple containing one or more references has unexpected behavior with regards to construction and assignment (especially copy/move construction and copy/move assignment). It's different from the behavior of both…
xcvr
  • 939
  • 8
  • 15
13
votes
2 answers

Enable std::get support on class

What are the templates that I have to specialize to support std::get? struct MyClass { int a; }; template struct MyContainer { MyClass array[I]; }; What do I have to specialize to be able to do: MyContainer<16>…
Matt Clarkson
  • 14,106
  • 10
  • 57
  • 85
12
votes
3 answers

Concatenating tuples as types

I'm trying to practice some template programming. Maybe there's a standard way to do this, and I would be thankful for such answers, but my main goal is to practice the template programming techniques, so I tried to implement it myself: I need to…
egst
  • 1,605
  • 3
  • 18
  • 25
12
votes
1 answer

What should tuple_map return?

I want to implement a generic tuple_map function that takes a functor and an std::tuple, applies the functor to every element of this tuple and returns an std::tuple of results. The implementation is pretty straightforward, however the question…
Evg
  • 25,259
  • 5
  • 41
  • 83
12
votes
3 answers

Different behavior when trying to swap two variables using {} and std::make_pair()

I was trying to swap two variables using std::tie() as per the following code (I am aware of std::swap, I was just trying this out of curiosity): #include #include using std::cin; using std::tie; using std::cout; using…
Shubham
  • 2,847
  • 4
  • 24
  • 37
12
votes
1 answer

Why can't std::tuple be element-wise constructed with a std::tuple of compatible types?

I can't initialize std::tuple elements element-wise from a std::tuple of compatible types. Why doesn't it work as with boost::tuple? #include #include template struct Foo { // error: cannot convert…
LogicStuff
  • 19,397
  • 6
  • 54
  • 74
12
votes
1 answer

Optimal way to access std::tuple element in runtime by index

I have function at designed to access std::tuple element by index specified in runtime template inline typename std::enable_if<_Index == std::tuple_size<_Tuple>::value,…
sliser
  • 1,645
  • 11
  • 15
12
votes
3 answers

How does std::get() work with std::tuple?

After trying to make a std::get(std::tuple) method myself, I'm not so sure how it's implemented by compilers. I know std::tuple has a constructor like this: tuple(Args&&... args); But what exactly is args... assigned to? I think this is useful…
Me myself and I
  • 3,990
  • 1
  • 23
  • 47
11
votes
2 answers

Initialize std::tuple with classes which have two or more arguments

#include class NoCopyMove { public: NoCopyMove(int a) : a_(a), b_(a) {} NoCopyMove(int a, int b) : a_(a), b_(b) {} NoCopyMove(const NoCopyMove&) = delete; NoCopyMove& operator=(const NoCopyMove&) = delete; …
Daniel Lee
  • 205
  • 3
  • 7
11
votes
2 answers

Why do I not get guaranteed copy elision with std::tuple?

I would expect that in C++20 the following code prints nothing between prints of A and B (since I expect guaranteed RVO to kick in). But output is: A Bye B C Bye Bye So presumably one temporary is being created. #include #include…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
11
votes
1 answer

Generic comparison operator for structs

In many of my unit tests I need to compare the contents of simple structs having only data members: struct Object { int start; int stop; std::string message; } Now, if I want to write something like: CHECK(object1==object2); I always have to…
Aleph0
  • 5,816
  • 4
  • 29
  • 80
11
votes
1 answer

Confusion while deriving from std::tuple, can not handle std::get

My basic idea was to derive my own class from std::tuple to get some helper types inside like this: template class TypeContainer: public std::tuple { public: using BaseType = std::tuple; static…
Klaus
  • 24,205
  • 7
  • 58
  • 113
11
votes
6 answers

Constructor arguments from tuple

Suppose I have a template which is parametrized by a class type and a number of argument types. a set of arguments matching these types are stored in a tuple. How can one pass these to a constructor of the class type? In almost C++11…
MvG
  • 57,380
  • 22
  • 148
  • 276
10
votes
1 answer

How to efficiently roll out a sequence using c++ templates

I've a complex type C depending on a template parameter which I need in a (length bounded) sequence. A constexpr function next() is available to go from C_n -> C_n+1. As every sequence element has a different type I'm using a std::tuple to store the…
Axel
  • 103
  • 4
9
votes
1 answer

Structured binding violations

The code as follows #include int main() { auto [a] = std::make_tuple(1); return [a]() -> int { return a; }(); } produces an error in clang 12: :6:13: error: 'a' in capture list does not name a variable return [a]() ->…
Fedor
  • 17,146
  • 13
  • 40
  • 131
1 2
3
26 27