gcc complex constant folding

前端 未结 4 1833
失恋的感觉
失恋的感觉 2021-01-12 03:10

It seems that gcc has some limitation on complex constant folding. Here is an example:

static inline unsigned int DJBHash(const char *str)
{
   int i;
   uns         


        
4条回答
  •  感情败类
    2021-01-12 04:01

    Perhaps C++ TMP might be able to do it. I'm not sure though.

    It is possible if you don't mind using variadic character literal lists instead of string literals:

    #include 
    #include 
    
    template
    struct DJBhash_helper
         : std::integral_constant {};
    
    template
    struct DJBhash_helper
         : DJBhash_helper<(acc << 5) + acc + head, tail...> {};
    
    template
    struct DJBhash
         : DJBhash_helper<5381, str...> {};
    
    int main()
    {
        std::cout << DJBhash<'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                             '0', '1', '2', '3', '4', '5', '6', '7'>::value << '\n';
    }
    

    ideone live demo

提交回复
热议问题