// Copyright (C) 2016-2018 T. Zachary Laine // // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) //[ map_assign #include #include #include // This transform applies all the call-subexpressions in a map_list_of // expression (a nested chain of call operations) as a side effect; the // expression returned by the transform is ignored. template struct map_list_of_transform { template auto operator() (boost::yap::expr_tag, Fn const & fn, Key2 && key, Value2 && value) { // Recurse into the function subexpression. Remember, transform() // walks the nodes in an expression tree looking for matches. Once it // finds a match, it is finished with that matching subtree. So // without this recursive call, only the top-level call expression is // matched by transform(). boost::yap::transform( boost::yap::as_expr(fn), *this); map.emplace( std::forward(key), std::forward(value) ); // All we care about are the side effects of this transform, so we can // return any old thing here. return 0; } std::map & map; }; // A custom expression template type for map_list_of expressions. We only // need support for the call operator and an implicit conversion to a // std::map. template struct map_list_of_expr { static boost::yap::expr_kind const kind = Kind; Tuple elements; template operator std::map () const { std::map retval; map_list_of_transform transform{retval}; boost::yap::transform(*this, transform); return retval; } BOOST_YAP_USER_CALL_OPERATOR_N(::map_list_of_expr, 2) }; // A tag type for creating the map_list_of function terminal. struct map_list_of_tag {}; auto map_list_of = boost::yap::make_terminal(map_list_of_tag{}); int main() { // Initialize a map: std::map op = map_list_of ("<", 1) ("<=",2) (">", 3) (">=",4) ("=", 5) ("<>",6) ; std::cout << "\"<\" --> " << op["<"] << std::endl; std::cout << "\"<=\" --> " << op["<="] << std::endl; std::cout << "\">\" --> " << op[">"] << std::endl; std::cout << "\">=\" --> " << op[">="] << std::endl; std::cout << "\"=\" --> " << op["="] << std::endl; std::cout << "\"<>\" --> " << op["<>"] << std::endl; return 0; } //]