Alternatives for compile-time floating-point initialization

牧云@^-^@ 提交于 2019-12-03 22:36:36

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!