25

I want to do something like this:

template<int N>
char* foo() {
  // return a compile-time string containing N, equivalent to doing
  // ostringstream ostr; 
  // ostr << N;
  // return ostr.str().c_str();
}

It seems like the boost MPL library might allow this but I couldn't really figure out how to use it to accomplish this. Is this possible?

6
  • Does it need to be a function like that? You can do it more easily with a pre-processor macro (you'd only lose type-safety).
    – vanza
    Jul 15, 2011 at 21:39
  • itoa() function will do the trick. cplusplus.com/reference/clibrary/cstdlib/itoa
    – Ram
    Jul 15, 2011 at 21:39
  • 1
    Define a compile time string... std::string is a run-time one. You can do semi-runtime magic, but not pure compile-time. Preprocessor is your best best.
    – user405725
    Jul 15, 2011 at 21:41
  • 1
    @Ram: The OP wants the conversion during compile time. The itoa function works during run-time. Jul 15, 2011 at 21:46
  • To clarify, getting a const char* would be fine.
    – Andrey
    Jul 15, 2011 at 21:55

6 Answers 6

30

First of all, if usually you know the number at run time, you can as easily build the same string. That is, if you have 12 in your program, you can have also "12".

Preprocessor macros can add also quotes to arguments, so you can write:

#define STRINGIFICATOR(X) #X

This, whenever you write STRINGIFICATOR(2), it will produce "2".

However, it actually can be done without macros (using compile-time metaprogramming). It is not straightforward, so I cannot give the exact code, but I can give you ideas on how to do it:

  1. Write a recursive template using the number to be converted. The template will recurse till the base case, that is, the number is less than 10.
  2. In each iteration, you can have the N%10 digit to be converted into a character as T.E.D. suggests, and using mpl::string to build the compile-time string that appends that character.
  3. You'll end up building a mpl::string, that has a static value() string.

I took the time to implement it as a personal exercise. Not bad at the end:

#include <iostream>
#include <boost/mpl/string.hpp>

using namespace boost;

// Recursive case
template <bool b, unsigned N>
struct int_to_string2
{
        typedef typename mpl::push_back<
                typename int_to_string2< N < 10, N/10>::type
                                         , mpl::char_<'0' + N%10>
                                         >::type type;
};

// Base case
template <>
struct int_to_string2<true,0>
{
        typedef mpl::string<> type;
};


template <unsigned N>
struct int_to_string
{
        typedef typename mpl::c_str<typename int_to_string2< N < 10 , N>::type>::type type;
};

int
main (void)
{
        std::cout << int_to_string<1099>::type::value << std::endl;
        return 0;
}
7
  • 1
    cool, I was just working on this but didn't know how to concatenate the chars :) Jul 15, 2011 at 22:22
  • @yi_H: mpl provides the type push_front<string_type,char>::type to define a compile-time string that has char prepended to an existing string. Jul 15, 2011 at 22:32
  • @Diego : Your code doesn't appear to handle negative values. Also, for those curious, I wrote the reverse of this code (compile time string-to-integral using mpl::string) some time ago; the code can be found in this answer.
    – ildjarn
    Jul 18, 2011 at 22:03
  • @ildjam: Yes, it does not cover negative numbers. I just did this in spare time just to practice. It won't be too difficult to add negative number support, AFAIK. Jul 18, 2011 at 23:42
  • This is pretty awesome. I shudder to think how much large numbers would inflate compile time... unless I'm missing something. May 31, 2012 at 14:01
22

I know this question is a few years old now, but I wanted a solution using pure C++11, with no boost dependency. So here is some code (with ideas borrowed from this answer to a different question):

/* IMPLEMENTATION */

/* calculate absolute value */
constexpr int abs_val (int x)
    { return x < 0 ? -x : x; }

/* calculate number of digits needed, including minus sign */
constexpr int num_digits (int x)
    { return x < 0 ? 1 + num_digits (-x) : x < 10 ? 1 : 1 + num_digits (x / 10); }

/* metaprogramming string type: each different string is a unique type */
template<char... args>
struct metastring {
    const char data[sizeof... (args)] = {args...};
};

/* recursive number-printing template, general case (for three or more digits) */
template<int size, int x, char... args>
struct numeric_builder {
    typedef typename numeric_builder<size - 1, x / 10, '0' + abs_val (x) % 10, args...>::type type;
};

/* special case for two digits; minus sign is handled here */
template<int x, char... args>
struct numeric_builder<2, x, args...> {
    typedef metastring<x < 0 ? '-' : '0' + x / 10, '0' + abs_val (x) % 10, args...> type;
};

/* special case for one digit (positive numbers only) */
template<int x, char... args>
struct numeric_builder<1, x, args...> {
    typedef metastring<'0' + x, args...> type;
};

/* convenience wrapper for numeric_builder */
template<int x>
class numeric_string
{
private:
    /* generate a unique string type representing this number */
    typedef typename numeric_builder<num_digits (x), x, '\0'>::type type;

    /* declare a static string of that type (instantiated later at file scope) */
    static constexpr type value {};

public:
    /* returns a pointer to the instantiated string */
    static constexpr const char * get ()
        { return value.data; }
};

/* instantiate numeric_string::value as needed for different numbers */
template<int x>
constexpr typename numeric_string<x>::type numeric_string<x>::value;

/* SAMPLE USAGE */

#include <stdio.h>

/* exponentiate a number, just for fun */
static constexpr int exponent (int x, int e)
    { return e ? x * exponent (x, e - 1) : 1; }

/* test a few sample numbers */
static constexpr const char * five = numeric_string<5>::get ();
static constexpr const char * one_ten = numeric_string<110>::get ();
static constexpr const char * minus_thirty = numeric_string<-30>::get ();

/* works for any constant integer, including constexpr calculations */
static constexpr const char * eight_cubed = numeric_string<exponent (8, 3)>::get ();

int main (void)
{
    printf ("five = %s\n", five);
    printf ("one ten = %s\n", one_ten);
    printf ("minus thirty = %s\n", minus_thirty);
    printf ("eight cubed = %s\n", eight_cubed);

    return 0;
}

Output:

five = 5
one ten = 110
minus thirty = -30
eight cubed = 512
1
  • How to call this from loop in constexpr function? numeric_builder<i> and my compiler says argument is not constant expression? Feb 4, 2019 at 5:42
13

This can be done using C++14 without any external dependencies. The key addition to the standard is the ability to have non-trivial constexpr constructors, allowing the functionality to be contained within a simple class.

Given an integer template parameter, the constructor can carry out the integer-to-string conversion. This is stored in a member character buffer whose size is determined by an additional constexpr function. Then, a user-defined conversion provides access to the buffer:

#include <cstdint>

template<std::intmax_t N>
class to_string_t {

    constexpr static auto buflen() noexcept {
        unsigned int len = N > 0 ? 1 : 2;
        for (auto n = N; n; len++, n /= 10);
        return len;
    }

    char buf[buflen()] = {};

public:
    constexpr to_string_t() noexcept {
        auto ptr = buf + buflen();
        *--ptr = '\0';

        if (N != 0) {
            for (auto n = N; n; n /= 10)
                *--ptr = "0123456789"[(N < 0 ? -1 : 1) * (n % 10)];
            if (N < 0)
                *--ptr = '-';
        } else {
            buf[0] = '0';
        }
    }

    constexpr operator const char *() const { return buf; }
};

Finally, a variable template (another C++14 addition) simplifies the syntax:

template<std::intmax_t N>
constexpr to_string_t<N> to_string;

puts(to_string<62017>); // prints "62017"

The functionality can be extended to support other bases (e.g. hexadecimal), wide character types, and the common container interface; I've packed this all into a single header and put it on GitHub at tcsullivan/constexpr-to-string.

With C++20, this can also be extended to support floating-point numbers. A container type is needed for the floating-point literal, which previously could not be a template parameter. See the f_to_string.hpp header in the GitHub repo for the implementation.

4
  • Excellent! That works great. The only small thing is to be able to choose between upper and lower for hexadecimal. Jun 6, 2022 at 17:33
  • Switched the accepted answer to this one since it's more useful in modern C++
    – Andrey
    Jul 29, 2022 at 1:49
  • 1
    This is effectively a link-only answer to your code on Github. Nothing in this answer shows how the conversion code actually works, or what C++17 features it uses. (Presumably extensions to what's allowed with constexpr.) It would be nice to at least show a minimal version in the answer itself. Feb 5 at 20:24
  • 1
    @PeterCordes You're right, this needed a real explanation. I've updated my answer to be more thorough, and discovered that a C++14 implementation (now shown) is possible.
    – clyne
    Feb 8 at 16:05
7

Maybe i missed something, but this should be as simple as:

 #define NUM(x) #x

Unfortunately this won't work with non-type template parameters.

3
  • 6
    This doesn't work for arbitrary compile time integers, though. NUM(1+1) gives "1+1".
    – hammar
    Jul 15, 2011 at 22:08
  • 2
    To allow other macros to be used as parameters, I recommend extra indirection: #define _NUM(x) #x followed by #define NUM(x) _NUM(x).
    – vanza
    Jul 15, 2011 at 22:09
  • 5
    @hammar enums and known constant compile-time values don't work either. eg NUM(foo); where enum { foo = 42 };. The macro will produce "foo" instead of "42".
    – greatwolf
    Jun 26, 2013 at 2:45
4

One trick I've seen done in situations where you know for a fact you will never have a number outside the range 0..9 is the following:

return '0' + N;

At first blush this is annoyingly limited. However, I'm surprised how many times this condition holds.

Oh, and I'm aware this returns a char rather than a std::string. This is a feature. string isn't a built in language type, so there's no way to create one at compile time.

3
  • 2
    Yeah in this case the integer could be anything.
    – Andrey
    Jul 15, 2011 at 21:55
  • 2
    This fails the requirement of returning a char *
    – M.M
    Jul 4, 2014 at 5:07
  • 1
    @M.M - A woefully underappreciated part of requirement analysis is to recognize what's really a requirement, vs. what's just an assumption based on implementation expectations. In this case the question literally says "do something like this". The above does in fact do something like that.
    – T.E.D.
    Sep 14, 2022 at 13:34
3

Another useful option:

template <int i, bool gTen>
struct UintToStrImpl
{
   UintToStrImpl<i / 10, (i > 99)> c;
   const char c0 = '0' + i % 10;
};

template <int i>
struct UintToStrImpl <i, false> 
{ 
   const char c0 = '0' + i; 
};

template <int i, bool sign>
struct IntToStrImpl
{
   UintToStrImpl<i, (i > 9)> num_;
};

template <int i>
struct IntToStrImpl <i, false>
{
   const char sign = '-';
   UintToStrImpl<-i, (-i > 9)> num_;
};

template <int i>
struct IntToStr
{
   IntToStrImpl<i, (i >= 0)> num_;
   const char end = '\0';
   const char* str = (char*)this;
};

std::cout << IntToStr<-15450>().str;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.