User defined literal arguments are not constexpr?

后端 未结 5 1082
余生分开走
余生分开走 2021-01-01 11:43

I\'m testing out user defined literals. I want to make _fac return the factorial of the number.

Having it call a constexpr function works,

5条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-01 12:20

    @Pubby. The easy way to digest the char non-type parameter pack is to cature it into an initializer list for a string. Then you can use atoi, atof, etc:

    #include 
    
    template
      int
      operator "" _suffix()
      {
        const char str[]{Chars..., '\0'};
        return atoi(str);
      }
    
    int
    main()
    {
      std::cout << 12345_suffix << std::endl;
    }
    

    Remember to tack on a null character for the C-style functions.

提交回复
热议问题