3 ms·
> if I want functional style code in C++, I have to define my functions as classes Much of the benefit of C++11 and Boost is in not having to write functor cla
by quotemstr 15y ago
> if I want functional style code in C++, I have to define my functions as classes
Much of the benefit of C++11 and Boost is in not having to write functor classes.
- scott_s 15y agoLambdas buy us this in C++11 (which is awesome), but not so much with Boost alone. Boost can help if you are, say, applying the member function of a class to a bunch of objects; you don't have to write a speciality function object to express that. But if you're writing logic from scratch, and you want it to be a closure, it needs to be a function object.
- quotemstr 15y agoYes, but many function objects can be constructed easily enough with Boost.Lambda and related libraries.
- scott_s 15y agoOy, Boost.Lambda. I tried, I really tried. I wrote this comment here on HN a year and a half ago: I played with boost::lambda, and I found the resulting code less clear than a simple for loop. I also had difficulty gaining an intuition for what I could and could not do with it. Rather than constantly asking myself "Can I use boost::lambda here...? Will it look better or worse than the alternative?" I just decided to never use it. While I prefer a functional style of coding, I recognized that it was silly to adhere to it if doing so made my code worse - C++'s support for what I wanted to do just wasn't there. If what I wanted to accomplish was several lines of code, I wrote a separate function or function object. If it was brief and I could not express it with combinations of existing functions, then I just wrote a for loop.
- SamReidHughes 15y agoIf you're writing the logic from scratch and want a function object, make a function whose arguments are the variables you need to close over and then use boost::bind. That's the most convenient way.