Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8479087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:55:39+00:00 2026-06-10T18:55:39+00:00

https://gist.github.com/2934374 So I was intrigued by this comparison and also by the awful C++

  • 0

https://gist.github.com/2934374

So I was intrigued by this comparison and also by the awful C++ solutions that appeared in the comments, but I must admit I’m having trouble to write a nice solution myself 🙂

My current attempt looks like this, the problems are:

  • the environment is copied when the tree is created, I would like the variables to be changeable after the tree is created
  • it still looks pretty ugly

So how could I squish it a bit and bind the variables so they would be affected by changes?

#include <map>
#include <string>
#include <iostream>
#include <functional>
using namespace std;

/* environ */
map<string,int> variables = { { "a" , 3 }, { "b", 4 }, { "c", 5 } };

function<int(int,int)> add = [] (int lp, int rp) { return lp + rp; };
function<int(int,int)> mlt = [] (int lp, int rp) { return lp * rp; };

/* impl */
struct Var {
    Var(int v) : p_v(v) {};
    int eval() { return p_v; };

private:
    int p_v;
};

template <typename LP, typename RP>
struct Op {
    Op(function<int(int,int)> op, LP lp, RP rp) : p_op(op), p_l(lp), p_r(rp) {};
    int eval() { return p_op(p_l.eval(), p_r.eval()); }
private:
    function<int(int,int)> p_op;
    LP p_l;
    RP p_r;
};

Var var(int val) { return Var(val); }

template <typename LP, typename RP>
auto op(function<int(int,int)> op, LP lp, RP rp) -> Op<LP,RP>
{
    return Op<LP,RP>(op,lp,rp);
}

Var operator "" _var(const char *key, size_t length)
{ return Var(variables[key]); }

/* gcc is failing me
Var operator "" _num(int val)
{ return Var(val); }
*/

int main()
{
    auto tree = op ( add, "a"_var, op ( mlt, var ( 2 ), "b"_var ));
    cout << tree.eval() << endl;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:55:41+00:00Added an answer on June 10, 2026 at 6:55 pm

    You’re re-inventing the Standard bind mechanism. There’s no need for any glue code to be written by the user.

    auto tree = bind(plus<int>(), ref(variables["a"]), bind(multiplies<int>(), 2, ref(variables["b"])));
    cout << tree();
    

    http://liveworkspace.org/code/f06fd83b5d7bcbf4829306d4e590da38

    std::ref makes it a reference, not a value- this means that you could bind a mutating operation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Folks, I have this code https://gist.github.com/2473387 But, how do I get the coordinates of
I have added this fix https://gist.github.com/2382288 for protect all fields against mass assignment in
I just posted this to a gist: https://gist.github.com/2228570 var out = ''; function doWhat(){
UPDATE 9th june 2012: Setup with mongoid 3.0.0.rc at heroku, see this gist: https://gist.github.com/2900804
I have a ivy.xml - https://gist.github.com/1898060 I also have the jar file related to
The following gist: https://gist.github.com/1876791 (borrowed from the demo in this question ) is a
i have a xml file like this : https://gist.github.com/1966260 。 I want to dump
Take a look at this programming challenge. https://gist.github.com/1083219 We’ve built a new communication protocol
Consider this gist: https://gist.github.com/752934 When converting from String to Time everything goes well. It
Using this code snippet: https://gist.github.com/1854699 . I have written this code Base.html: https://gist.github.com/3438843 index.html:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.