C++ compile time function execution

后端 未结 6 1663
醉酒成梦
醉酒成梦 2021-01-02 02:41

I have string tags in my code that are converted to numbers and used to search values in a tag-value structure.

I have something like this:

void foo(         


        
6条回答
  •  萌比男神i
    2021-01-02 03:14

    Not sure that you can. Is the list of possible tags small? Even if not, is it small most of the time. If so, you can use template specialization on a subset of the tags

    template 
    int toNumber() {
        return toNumber(tag);
    }
    
    template<>
    int toNumber<"0">() {
         return 0;
    }
    
    template<>
    int toNumber<"1">() {
         return 1;
    }
    

    (caveats: my specialization syntax might be wrong, and I have no idea if this works for char*)

提交回复
热议问题