Yesterday I read a blog entry
about converting a compile time known function argument from a constexpr function to a type like std::integral_constant<>.
A possible usage example is to convert types from user defined literals.
Consider the following example:
constexpr auto convert(int i)
{
return std::integral_constant<int, i>{};
}
void test()
{
// should be std::integral_constant<int, 22>
using type = decltype(convert(22));
}
But obviously and as expected Clang throws the following error:
error: ‘i’ is not a constant expression
return std::integral_constant<int, i>{};
^
The Author of the mentioned blog proposed to use a templated user defined literal to split the number into a std::integer_sequence to parse it into an int.
But this suggestion seems not useable for me.
Is there an efficient way to convert a compile time known function argument into a type like std::integral_constant<>?
using type = decltype(convert(22));more readable than an alias template (or a variable template)?using type = decltype(convert(22))was just an example how to extract the type of the given expression. I thought about possibilities to parse the expressionconvert(22)into a compile time AST expression which could be evaluated from other templates without using macros or user defined literals.