

If they don't fit into an int, they'll try long, long long etc. Normal C++ literals automatically adjust their types according to their value. IIRC, it's not guaranteed that the letters are contiguous in the basic execution character set (as opposed to the digits), so this could be rather painful w/o a switch or lookup-table. Your parse function currently rejects hexadecimal, octal, and in C++1y binary literals, as well as C++1y's digit separators. The shifting (in base 10) will be done on the fly.Ĭonstexpr int combine(int val, int p0, TT. Algorithmīy changing the order of operations, you can get rid of the pow function (template) entirely: Pass the current result to the next step, then multiply and add. Using constexpr functions might reduce the number of instantiations to a minimum of 1 (the literal operator template) or two (the constexpr function template with a single template type parameter). Note that I didn't measure this, so take it with a grain of salt: lightweight constexpr functions and alias template can be faster to instantiate. I would try to avoid using full-blown class templates when there's an elegant alternative solution with alias templates or constexpr functions.
#Flysketch review code
Is there a way to improve this code and/or make it cleaner or more idiomatic? Have I missed some potential flaws? To generate other integral constants, I plan to add the user defined literals _cl, _cll, _cu, _cul and _cull whoe implementation is exactly the same, only the resulting type differs. Here is a small working example: int main() With this literal, wirting 42_c generates an instance of std::integral_constant. Specialization of parse to parse a single (exponent > 0) ? pow_helper(value, value, exponent) : Compile-time pow function, only works with Here is the implementation: #include Ĭonstexpr auto pow_helper(T acc, T value, U times) Basically, the goal is to allow users to write std::integral_constant instances without the usual boilerplate. I created a user defined literal _c to convert an "integer" literal into an std::integral_constant.
