你可能會忽略「的printf」功能的風格,並使用一個流處理器(採取可變數量的參數(可變參數模板)):
// Header
// ============================================================================
#include <iostream>
#include <sstream>
#include <tuple>
// Format
// ============================================================================
namespace Detail {
template<unsigned I, unsigned N>
struct Format;
template<unsigned N>
struct Format<N, N> {
template<typename... Args>
static void write(std::ostream&, const std::tuple<Args...>&, std::size_t offset)
{}
};
template<unsigned I, unsigned N>
struct Format {
template<typename... Args>
static void write(
std::ostream& stream,
const std::tuple<Args...>& values,
std::size_t offset)
{
if(offset == 0) stream << std::get<I>(values);
else Format<I+1, N>::write(stream, values, offset - 1);
}
};
class FormatParser
{
public:
const char* fmt;
const std::size_t size;
FormatParser(const char* fmt, std::size_t size)
: fmt(fmt), size(size)
{}
virtual ~FormatParser() {}
void write(std::ostream& stream) const;
protected:
virtual void write_value(std::ostream&, std::size_t index) const = 0;
};
} // namespace Detail
template<typename... Args>
class Format : public Detail::FormatParser
{
public:
typedef std::tuple<const Args&...> Tuple;
static constexpr std::size_t Size = std::tuple_size<Tuple>::value;
const std::tuple<const Args&...> values;
Format(const char* fmt, const Args&... values)
: Detail::FormatParser(fmt, Size), values(values...)
{}
protected:
void write_value(std::ostream& stream, std::size_t index) const {
Detail::Format<0, Size>::write(stream, values, index);
}
};
template <typename... Args>
inline Format<Args...> format(const char* fmt, const Args&... values) {
return Format<Args...>(fmt, values...);
}
template <typename... Args>
inline std::ostream& operator << (std::ostream& stream, const Format<Args...>& format) {
format.write(stream);
return stream;
}
template <typename... Args>
inline std::string format_string(const char* fmt, const Args&... values) {
std::ostringstream result;
result << format(fmt, values...);
return result.str();
}
// Source
// ============================================================================
#include <cctype>
#include <cstdlib>
#include <stdexcept>
namespace Detail {
void FormatParser::write(std::ostream& stream) const {
const char* s = fmt;
while(*s) {
switch(*s) {
case '{':
if(*(++s) != '{') {
char* end;
unsigned long index = std::strtoul(s, &end, 10);
while(*end != '}') {
if(! std::isspace(*end)) {
s = end;
if(! *s) s = "End";
throw std::runtime_error(std::string(
"Invalid Format String `") + fmt + "` at " + s);
}
++end;
}
s = end + 1;
if(index < size) write_value(stream, index);
else throw std::runtime_error(std::string(
"Invalid Format Index `") + std::to_string(index)
+ "` in `" + fmt + '`');
continue;
}
break;
case '}':
if(*(++s) != '}') {
if(! *s) s = "End";
throw std::runtime_error(
std::string("Invalid Format String `") + fmt + "`"
"Missing `}` at " + s);
}
break;
}
stream.put(*s++);
}
}
} // namespace Detail
// Usage
// ============================================================================
int main() {
// a = 1; b = 2; 1 + 2 = 3
std::cout << format("a = {0}; b = {1}; {0} + {1} = {2}", 1, 2, 3) << "\n";
}
另外:看一看boost::format
你能使用C++ 11? – WaeCo
@WaeCo是的我可以 – Vladp