1

I am using boost/cstdint.hpp in a C++ project because I am compiling in C++03 mode (-std=c++03) and I want to have fixed-width integers (they are transmitted over the network and stored to files). I am also using snprintf because it is a simple and fast way to format strings.

Is there a proper formatter to use boost::uint64_t with snprintf(...) or should I switch to another solution (boost::format, std::ostringstream) ?

I am current using %lu but I am not fully happy with it as it may not work on another architecture (where boost::uint64_t is not defined as long unsigned), defeating the purpose of using fixed-width integers.

boost::uint64_t id
id = get_file_id(...)
const char* ENCODED_FILENAME_FORMAT = "encoded%lu.dat";
//...
char encoded_filename[34];
snprintf(encoded_filename, 34, ENCODED_FILENAME_FORMAT, id);
Xion345
  • 1,627
  • 12
  • 26

1 Answers1

3

snprintf isn't a Boost function. It knows how to print the fundamental types only. If none of those coincides with boost::uint64_t, then it isn't even possible to print that.

In general, as you note the formatter has to match the underlying type. So even if it's possible, the formatter will be platform-dependent. There's no extension mechanism by which Boost can add new formatters to snprintf.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • In C++11 or C99, you have the header `` which defines the macro `PRIu64` , the proper formatter for a `uint64_t` . I was hoping boost would provide a similar mechanism. And a 64 bits unsigned integer is kindof a fundamental type. Please see this SO post : http://stackoverflow.com/questions/16859500/mmh-who-are-you-priu64 – Xion345 Sep 12 '13 at 11:58
  • @Xion345: That's a macro from the standard library applying to the same standard library. That's a lot easier. – MSalters Sep 12 '13 at 12:34
  • Ok, I see your point now. I was thinking boost could provide this type of mechanism as `uint64_t` is an alias for `unsigned`, `long unsigned` or `long long unsigned` on most platforms (so formatters for these types exist). So in addition to providing the proper alias for `uint64_t`, boost could also provide the proper alias for `PRIu64` (whether it is `%u`, `%lu`, `%llu`). – Xion345 Sep 12 '13 at 12:46