Alternatives for compile-time floating-point initialization

后端 未结 1 479
轮回少年
轮回少年 2021-02-18 17:40

I\'m currently working on a template-meta-programming based implementation of floating-point arithmetic. The template which represent compile-time float values is as follows:

1条回答
  •  不知归路
    2021-02-18 18:07

    You might want to use user-defined literals. According to cppreference.com, it

    Allows integer, floating-point, character, and string literals to produce objects of user-defined type by defining a user-defined suffix.

    (see also http://en.cppreference.com/w/cpp/language/user_literal). This way, you could make the expression

    123.456_mysuffix
    

    yield whatever type you want, if you define the literal operator for _mysuffix. With that operator, you can access the input 123.456 either as a (standard c++) floating point number or you can do the necessary conversion from the raw string as a const char* yourself.

    EDIT: After reading your edited question and realizing what kind of template meta-programming you were talking about, I just wanted to emphasize that the literal can also be accessed as a parameter pack of char template parameters. You might be able to integrate this into your compile time framework.

    0 讨论(0)
提交回复
热议问题