/* @copyright Louis Dionne 2015 Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) */ #include #include #include #include #include #include #include #include #include using namespace boost::hana; constexpr auto formats = make_map( make_pair(type, string<'%', 'd'>), make_pair(type, string<'%', 'f'>), make_pair(type, string<'%', 's'>) ); struct concat_strings { template constexpr auto operator()(_string, _string) const { return string; } }; template constexpr auto format(Tokens ...tokens_) { auto tokens = make_tuple(tokens_...); // If you don't care about constexpr-ness of `format`, you can use // this lambda instead of `compose(partial(...), decltype_)`: // // [](auto token) { // return formats[decltype_(token)]; // } auto format_string_tokens = adjust_if(tokens, compose(not_, is_a), compose(partial(at_key, formats), decltype_) ); auto format_string = fold.left(format_string_tokens, string<>, concat_strings{}); auto variables = filter(tokens, compose(not_, is_a)); return prepend(variables, format_string); } int main() { int a = 1; float b = 1.3; char const* c = "abcdef"; auto args = format( BOOST_HANA_STRING("first="), a , BOOST_HANA_STRING(" second="), b , BOOST_HANA_STRING(" third="), c ); unpack(args, [](auto fmt, auto ...args) { std::printf(to(fmt), args...); }); }