Created
July 26, 2013 23:12
-
-
Save jl2/6092861 to your computer and use it in GitHub Desktop.
std::tuple to string, 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> | |
| #include <tuple> | |
| template<uint N, uint MAX, typename... Args> struct tuple_formatter { | |
| static void format_tuple(boost::format &fmt, const std::tuple<Args...>& args) { | |
| fmt % std::get<N>(args); | |
| tuple_formatter<N+1,MAX,Args...>::format_tuple(fmt, args); | |
| } | |
| }; | |
| template<uint MAX, typename... Args> struct tuple_formatter<MAX,MAX,Args...> { | |
| static void format_tuple(boost::format &, const std::tuple<Args...>& ) { | |
| } | |
| }; | |
| template<typename... Args> boost::format &format_with_tuple(boost::format &fmt, const std::tuple<Args...>& args) { | |
| tuple_formatter<0, sizeof...(Args), Args...>::format_tuple(fmt, args); | |
| return fmt; | |
| } | |
| int main() { | |
| boost::format tmp("%3% %1% %2%"); | |
| std::cout << boost::str(format_with_tuple(tmp, std::make_tuple("omg", 45, 3.1415))) << "\n"; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment