Is it possible to generate a string at compile time?

c++, c++11, compile-time, string-formatting, templates

Solution

After a bit more digging around I came across this on SO: https://stackoverflow.com/a/24000041/897778

Adapted for my use case I get:

namespace detail
{
    template<unsigned... digits>
    struct to_chars { static const char value[]; };

    template<unsigned... digits>
    const char to_chars<digits...>::value[] = {'{', ('0' + digits)..., '}' , 0};

    template<unsigned rem, unsigned... digits>
    struct explode : explode<rem / 10, rem % 10, digits...> {};

    template<unsigned... digits>
    struct explode<0, digits...> : to_chars<digits...> {};
}

template<unsigned num>
struct num_to_string : detail::explode<num / 10, num % 10>
{};

template <unsigned N>
void test()
{
    const char* str = num_to_string<N>::value;
}

`boost::mpl` was also suggested, but this code seems simpler.

Problem

In the following example I use `snprintf` inside a template function to create a string that contains the value of the template parameter `N`. I would like to know if there is a way to generate this string at compile time instead. ``` template <unsigned N> void test() { char str[8]; snprintf(str, 8, "{%d}", N); } ```

Original source

Related problems