Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

The Way of the Exploding Tuple

Download

Right click “Save as…”

Zip

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.

 

Follow the Discussion

  • aloy banerjeealoy 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;
    }
    }

  • Michael KadanerMichael 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));

  • Paul GrokePaul 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.

  • nsfnsf

    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.

  • nsfnsf

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

  • WillWill

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

Remove this comment

Remove this thread

close

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.