Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are tuples of tuples allowed?

I'm currently working on a class with a lot of templates and being able to build tuples of tuples would make it a lot easier

But I tried this simple code in MSVC++ 2010:

#include <tuple>

void main() {
     auto x = std::make_tuple(std::make_tuple(5, true));
}

And I get a compilation error. The same problem happens if I don't use std::make_tuple but directly std::tuple's constructor.

Is it a bug of MSVC or are tuples of tuples not allowed by the standard?

like image 368
Tomaka17 Avatar asked Jul 15 '10 09:07

Tomaka17


People also ask

Can you have a tuple of tuples?

tuple is immutable, but you can concatenate multiple tuples with the + operator. At this time, the original object remains unchanged, and a new object is generated.

Can tuples have 3 values?

Creating a TupleA tuple can have any number of items and they may be of different types (integer, float, list, string, etc.).

Can we concatenate two tuples?

Method #1 : Using + operator This is the most Pythonic and recommended method to perform this particular task. In this, we add two tuples and return the concatenated tuple. No previous tuple is changed in this process.

Can you zip two tuples?

12.5 Lists and tupleszip is a built-in function that takes two or more sequences and “zips” them into a list of tuples where each tuple contains one element from each sequence. In Python 3, zip returns an iterator of tuples, but for most purposes, an iterator behaves like a list.


2 Answers

More data points:

  • If we use std::tr1::tuple and explicitly state the type instead of using auto, then Visual C++ 2008 compiles the code without error. Trying to compile that same code with Visual C++ 2010 results in the error you are seeing.

  • If we use boost::tuple an explicitly state the type instead of using auto, then Visual C++ 2008 and Visual C++ 2010 both compile the code without error.

It looks like it is probably an implementation bug.

like image 90
James McNellis Avatar answered Sep 24 '22 18:09

James McNellis


You've making a tuple with only one member --- doesn't that defeat the purpose of tuples? Anyway, I suspect that's leading to ambiguity.

make_tuple combines type inference with a call to the tuple constructor. When the tuple constructor is called with a single argument which is also a tuple, it's possible that a converting constructor is a better fit than a wrapping constructor. Hence the problem.

Tuples of tuples are allowed. 1-Tuples might not be.

like image 23
Ben Voigt Avatar answered Sep 22 '22 18:09

Ben Voigt