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);
4

1 回答 1

3

snprintf不是 Boost 功能。它只知道如何打印基本类型。如果这些都不符合boost::uint64_t,那么甚至不可能打印出来。

通常,正如您所注意到的,格式化程序必须匹配基础类型。因此,即使有可能,格式化程序也将依赖于平台。Boost 没有扩展机制可以将新的格式化程序添加到snprintf.

于 2013-09-12T11:11:19.360 回答