Last active
February 12, 2018 03:42
-
-
Save gchudnov/6a90d51af004d97337ec to your computer and use it in GitHub Desktop.
C++ variadic formatter using boost::format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <boost/format.hpp> | |
inline std::string format_str_recursive(boost::format& message) { | |
return message.str(); | |
} | |
template<typename TValue, typename... TArgs> | |
std::string format_str_recursive(boost::format& message, TValue&& arg, TArgs&&... args) { | |
message % std::forward<TValue>(arg); | |
return format_str_recursive(message, std::forward<TArgs>(args)...); | |
} | |
template<typename... TArgs> | |
std::string format_str(const char* fmt, TArgs&&... args) { | |
boost::format message(fmt); | |
return format_str_recursive(message, std::forward<TArgs>(args)...); | |
} | |
int main() { | |
std::cout << format_str("Boost %s %d.%d.%d", "version", 1, 57, 0) << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment