User defined literal for std::integral_constant I created a user defined literal _c to convert an "integer" literal into an std::integral_constant. Basically, the goal is to allow users to write std::integral_constant instances without the usual boilerplate. Here is the implementation:

#include &lt;type_traits&gt;

template&lt;typename T, typename U&gt;
constexpr auto pow_helper(T acc, T value, U times)
    -&gt; T
{
    return (times &gt; 1) ?
        pow_helper(acc*value, value, times-1) :
            acc;
}

// Compile-time pow function, only works with
// an unsigned integer exponent
template&lt;typename T, typename U&gt;
constexpr auto pow(T value, U exponent)
    -&gt; T
{
    return (exponent == 0) ? 1 :
        (exponent &gt; 0) ? pow_helper(value, value, exponent) :
            1 / pow_helper(value, value, -exponent);
}

// Structure to parse an integer literal
template&lt;typename Integral, char C, char... Digits&gt;
struct parse
{
    static constexpr Integral value =
        parse&lt;Integral, C&gt;::value * pow(10u, sizeof...(Digits))
        + parse&lt;Integral, Digits...&gt;::value;
};

// Specialization of parse to parse a single
// decimal digit
template&lt;typename Integral, char C&gt;
struct parse&lt;Integral, C&gt;
{
    static_assert(C &gt;= '0' &amp;&amp; C &lt;= '9',
                  "only characters in range 0..9 are accepted");

    static constexpr Integral value = C - '0';
};

// User defined literal for std::integral_constant
template&lt;char... Digits&gt;
constexpr auto operator"" _c()
    -&gt; std::integral_constant&lt;int, parse&lt;int, Digits...&gt;::value&gt;
{
    return {};
}


With this literal, wirting 42_c generates an instance of std::integral_constant&lt;int, 42&gt;. Here is a small working example:

int main()
{
    std::cout &lt;&lt; 45_c &lt;&lt; '\n';   // prints 45
    std::cout &lt;&lt; -23_c &lt;&lt; '\n';  // prints -23

    static_assert(std::is_same&lt;decltype(58_c), std::integral_constant&lt;int, 58&gt;&gt;::value, "");
}


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.

Is there a way to improve this code and/or make it cleaner or more idiomatic? Have I missed some potential flaws?
Usage of "fat" templates

I would try to avoid using full-blown class templates when there's an elegant alternative solution with alias templates or constexpr functions. 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. 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).

Algorithm

By 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. The shifting (in base 10) will be done on the fly.

Sketch:

constexpr int combine(int p)
{
    return p;
}

template&lt;class... TT&gt;
constexpr int combine(int val, int p0, TT... pp)
{
    return combine(val*10 + p0, pp...);
}

constexpr int parse(char C)
{
    return (C &gt;= '0' &amp;&amp; C &lt;= '9')
           ? C - '0'
           : throw std::out_of_range("only decimal digits are allowed");
}

template&lt;char... Digits&gt;
constexpr auto operator"" _c()
-&gt; std::integral_constant&lt;int, combine(0, parse(Digits)...)&gt;
{
    return {};
}


Literals in other bases

Your parse function currently rejects hexadecimal, octal, and in C++1y binary literals, as well as C++1y's digit separators. Supporting the bases &lt; 10 is fairly simple because you only need to change the "shifting" factor. I.e. multiply by 8 or 2 instead of 10. For base 16, you also need to add some code to the character -> digit conversion. 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. Also, upper/lower case.

Automatically growing literal type

Normal C++ literals automatically adjust their types according to their value. If they don't fit into an int, they'll try long, long long etc. Maybe one of the user-defined literals should mimic that behaviour for convenience.

Operators

Unfortunately, the StdLib doesn't provide operators for std::integral_constant. Therefore, -23_c will not be a std::integral_constant&lt;int, -23&gt; but rather an int (via the implicit conversion operator). I find that surprising.

Consider using a custom type, possibly derived from / convertible to std::integral_constant and provide (metaprogramming) operators for this type.



Side remark:

lit&lt;45&gt; is much easier to implement:

template&lt;std::uintmax_t N&gt;
using lit = std::integral_constant&lt;std::uintmax_t, N&gt;;