KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Let's say I have a few structs like this: struct MyStruct1 { inline void DoSomething() { cout << "I'm number one!" << endl; } }; struct MyStruct2 { static int DoSomething() { cout << "I'm the runner up." << endl; return 1; } }; struct MyStruct3 { void (*DoSomething)(); MyStruct3() { DoSomething = &InternalFunction; } static void InternalFunction() { cout << "I'm the tricky loser." << endl; } }; As you can see, for all three structs, I can call DoSomething() on an object of that struct and have it work (though this is achieved differently for each struct): MyStruct1 a; MyStruct2 b; MyStruct3 c; a.DoSomething(); // works, calls Struct1's instance function b.DoSomething(); // works, calls Struct2's static function, discards return value c.DoSomething(); // works, calls Struct3's function pointer Now, let's say I put an arbitrary selection of these structs into a tuple: tuple<MyStruct2, MyStruct3, MyStruct2, MyStruct1> collection; Let's also say that I want to take one of those elements and run its DoSomething() function based on an index that is determined at runtime. To achieve this, I could use a switch statement: switch(index) { case 0: get<0>(collection).DoSomething(); break; case 1: get<1>(collection).DoSomething(); break; case 2: get<2>(collection).DoSomething(); break; case 3: get<3>(collection).DoSomething(); break; } This works fine and dandy, but gets very tedious, repetitive and error-prone when it needs to be done with multiple differently arranged (and potentially much longer than 4-element) tuples. It would be very handy if a switch statement could be automatically generated based on the number of elements in a variadic template. Pseudocode:
Tags (comma-separated)
Save Edits
Cancel