Note that there are some explanatory texts on larger screens.

plurals
  1. POIs a compile-time checked string-to-int map possible?
    primarykey
    data
    text
    <p>I'm probably trying to achieve the impossible, but StackExchange always surprises me, so please have a go at this:</p> <p>I need to map a name to an integer. The names (about 2k) are unique. There will be no additions nor deletions to that list and the values won't change during runtime.</p> <p>Implementing them as <code>const int</code> variables gives me compile-time checks for existence and type. Also this is very clear and verbose in code. Errors are easily spotted.</p> <p>Implementing them as <code>std::map&lt;std::string, int&gt;</code> gives me a lot of flexibility for building the names to look up with string manipulation. I may use this to give strings as parameters to functions which than can query the list for multiple values by appending pre-/suffixes to that string. I can also loop over several values by creating a numeral part of the key name from the loop variable. </p> <p>Now my question is: is there a method to combine both advantages? The missing compile-time check (especially for key-existence) almost kills the second method for me. (Especially as <code>std::map</code> silently returns <code>0</code> if the key doesn't exist which creates hard to find bugs.) But the looping and pre-/suffix adding capabilities are so damn useful.</p> <p>I would prefer a solution that doesn't use any additional libraries like boost, but please suggest them nevertheless as I might be able to re-implement them anyway.</p> <p>An example on what I do with the map:</p> <pre><code>void init(std::map&lt;std::string, int&gt; &amp;labels) { labels.insert(std::make_pair("Bob1" , 45 )); labels.insert(std::make_pair("Bob2" , 8758 )); labels.insert(std::make_pair("Bob3" , 436 )); labels.insert(std::make_pair("Alice_first" , 9224 )); labels.insert(std::make_pair("Alice_last" , 3510 )); } int main() { std::map&lt;std::string, int&gt; labels; init(labels); for (int i=1; i&lt;=3; i++) { std::stringstream key; key &lt;&lt; "Bob" &lt;&lt; i; doSomething(labels[key.str()]); } checkName("Alice"); } void checkName(std::string name) { std::stringstream key1,key2; key1 &lt;&lt; name &lt;&lt; "_first"; key2 &lt;&lt; name &lt;&lt; "_last"; doFirstToLast(labels[key1.str()], labels[key2.str()]); } </code></pre> <p>Another goal is that the code shown in the <code>main()</code> routine stays as easy and verbose as possible. (Needs to be understood by non-programmers.) The <code>init()</code> function will be code-generated by some tools. The <code>doSomething(int)</code> functions are fixed, but I can write wrapper functions around them. Helpers like <code>checkName()</code> can be more complicated, but need to be easily debuggable. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload