How do I access a Tuple like an Array (with bracket overload and type safety)?


I am trying to build a Tuple class that can be accessed like an Array. I could probably turn things into (void *) but that would defeat the purpose of the templates since I'm trying to get type-safety.

I'm compiling using VS2010 pro. My current non-working solution produces the following error.

Error: 'Item &MyList::operator ' : could not deduce template argument for 'N'.

<pre class="lang-cpp prettyprint-override">#include <tuple> #include <stdio.h> template <int size, typename Ty0, typename Ty1=std::tr1::_Nil, typename Ty2=std::tr1::_Nil, typename Ty3=std::tr1::_Nil, typename Ty4=std::tr1::_Nil, typename Ty5=std::tr1::_Nil, typename Ty6=std::tr1::_Nil, typename Ty7=std::tr1::_Nil, typename Ty8=std::tr1::_Nil, typename Ty9=std::tr1::_Nil> struct MyList { std::tuple<Ty0, Ty1, Ty2, Ty3, Ty4, Ty5, Ty6, Ty7, Ty8, Ty9> items; template <int N, typename Ty> Ty &operator[](int N) { auto &var = std::get<N>(items); return var; } }; void main() { MyList<2, int, double> list; auto var = list[0]; }

<b>Potential Solutions: (edit)</b>

<ol><li><a href="http://cpptruths.blogspot.com/2012/01/array-like-access-and-iterators-for.html" rel="nofollow">Variadic templates with homogeneous data</a></li> <li><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3413.html" rel="nofollow">Using constexpr C++11</a></li> </ol>


It depends what you want to index the tuple <em>with</em>. If you're using a runtime integer then clearly you can't attain type safety; there's no way for the compiler to know what type to return (other in the case where the tuple is homogeneous, for which see above).

If on the other hand you're just after the syntax of subscripting, you can do this using indexing objects with appropriate types, e.g. (for exposition) the ready-made std::placeholders:

template<typename T> typename std::tuple_element<std::is_placeholder<T>::value, std::tuple<Ty0, Ty1, Ty2, Ty3, Ty4, Ty5, Ty6, Ty7, Ty8, Ty9>>::type & operator[](T) { return std::get<std::is_placeholder<T>::value>(items); }


using namespace std::placeholders; auto var = list[_1];


  • With templates: Operator resolved first or conversion resolved first?
  • Reference the second row in named table for data validation
  • C++: instantiate a templated class according to an argument
  • How to convert list into string with quotes in python
  • How can I write a recursive function to reverse a linked list?
  • Einsum optimize fails for basic operation
  • Invoking a controller's action by button in View without redirecting to any view
  • OpenCV Python: Draw minAreaRect ( RotatedRect not implemented)
  • Problems with matplotlib.pyplot.xticks()
  • Let a function return any type in C++ class
  • Disable Kendo Autocomplete
  • RxJava debounce by arbitrary value
  • Django invalid literal for int() with base 10
  • xtable package: Skipping some rows in the output
  • d3 v4 drag and drop with TypeScript
  • Time complexity of a program which involves multiple variables
  • Why does access(2) check for real and not effective UID?
  • C++ Partial template specialization - design simplification
  • Illegal mix of collations for operation for date/time comparison
  • Submit form in a displaytag pagination
  • How to model a transition system with SPIN
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • R: gsub and capture
  • Circular dependency while pushing http interceptor
  • Linker errors when using intrinsic function via function pointer
  • Python: how to group similar lists together in a list of lists?
  • Buffer size for converting unsigned long to string
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to get Windows thread pool to call class member function?
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Django query for large number of relationships
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal