Skip to content

Instantly share code, notes, and snippets.

@jefftrull
Last active August 29, 2015 14:18
Show Gist options
  • Save jefftrull/40a441bdb75365b2bf75 to your computer and use it in GitHub Desktop.
Save jefftrull/40a441bdb75365b2bf75 to your computer and use it in GitHub Desktop.
An attempt to construct a functional-style function value-returning function from a mutating function, using Phoenix
// test transforming a mutating function into a value-returning function via Phoenix
// i.e., void f(T& a) -> T f(T const& a) or T F(T a)
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <iostream>
#include <string>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/algorithm/string/case_conv.hpp>
void my_fn(std::string& s) {
boost::algorithm::to_lower(s);
}
int main() {
namespace phx = boost::phoenix;
using phx::local_names::_a;
using phx::arg_names::arg1;
using phx::placeholders::_1;
using phx::let;
using phx::bind;
std::string test("Foo");
// Attempt to make a function that copies its input, runs the mutator,
// and returns the result:
std::string result = let(_a = arg1)[bind(&my_fn, phx::ref(_a))](test);
// Unfortunately let doesn't have a return value...
std::cout << "result = " << test << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment