I have a function that takes an integer and returns a string that is picked by a switch. The strings are part of a coherent text and I want to be able to add cases to the switch whenever I want to edit the coherent text that the function returns when called for each consecutive int.
So how the function looks like right now:
std::string proceed(int id){
switch(id){
case 1: return "Hello";
case 2: return "my ";
case 3: return "is";
case 4: return "Marc";
}
}
I would like to be able to insert "Name" after case 2 and before case 3. What I do right now is insert another case 3 : return "Name"; and increase ALL the indices below by 1. Which is very fiddly and horrible workflow.
I am looking for a way to do something like the following, in a C++-legal way:
std::string proceed(int id){
int i = 0;
switch(id){
case ++i: return "Hello";
case ++i: return "my ";
case ++i: return "is";
case ++i: return "Marc";
}
}
here the case statement always (in code) uses the same symbols, which allows to just dump a case anywhere without adjusting ALL the cases.
Is there any syntax or tool to do this in a neat way? (besides using some separate program for automated text editing)
switch/case
. You can consider another design to make it more robust - like storing key-value pairs in amap
/unordered_map
.enum
s instead of hard coded integrals, then any change to their actual values will be reflected in thecase
statements. And you can insert new enum lables anywhere in the set without causing problems to the switch block.