The Way of the Exploding Tuple

Download this episode

Download Video

Description

People commonly ask: “If I have a tuple<T...>  and a function fun(T...), how do I generically call the function passing the tuple as arguments?” It’s a short piece of quite unusual code.

 

Download:

Zip

Day:

3

Level:

Code:

012

Embed

Format

Available formats for this video:

Actual format may change based on video formats available and browser capability.

    The Discussion

    • User profile image
      aloy banerjee

      what is wrong in following code whete i want to sum or concat the integer or string based on the input...Please help...thanks in advance

      public partial class wfrmGenerics : System.Web.UI.Page
      {
      protected void Page_Load(object sender, EventArgs e)
      {
      GetResult<int>(1, 2); GetResult<string>("1", "2");
      }
      public T GetResult<T>(T a, T b)
      {
      T c;
      c = a + b;
      }
      }

    • User profile image
      Michael Kadaner

      I'd like to be able to write something like this:

      void fun(int i, const char* s, double d);
      . . .
      auto t = make_tuple(1, "2", 3.0);
      . . .
      foo(explode(t));

    • User profile image
      Paul Groke

      The thing with the return type can be solved by just "piping" it through with decltype().
      Like this:

      http://ideone.com/nurq09

      I'm not 100% sure I got all the forward<> stuff right, but it seems to work fine.

    • User profile image
      nsf

      My solution to the problem, haven't tested it much regarding move semantics (the correctness of std::forward use), but it seems to work with all sorts of functors:

      http://ideone.com/abiKOo

      I'm not using the recursive approach, but an index generator based one. Only 40 lines of code.

    • User profile image
      nsf

      Updated it with a shorter version. 36 lines of code!

    • User profile image
      Will

      STL said he had two objections, but it seems that he could only tell the first. Will we know what the second was?

    Comments closed

    Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.