3 ms·
I don't know a lot of C++, but the examples that demonstrate how C++11 compares to C++98 look like a vast improvement. Until you come down to this monster:
by kleiba 15y ago
I don't know a lot of C++, but the examples that demonstrate how C++11 compares to C++98 look like a vast improvement. Until you come down to this monster:
auto i = find_if( begin(v), end(v), [=](int i) { return i > x && i < y; } );
This is the improved version by the way, but I'm not sure if a loop wouldn't be more readable for such a straight-forward problem. Plus I can't help but think that C++ will probably never win a beauty contest.
- CountHackulus 15y agoI don't see what the problem is here, this is very readable to me. You want to find between the beginning and end of a list called v, for a value between y < i < x. Admittedly the condition in the lambda could be better written, and the results should probably be called something other than i, but it's perfectly clear what's going on here. Let's look at the equivalent C++98 loop: int i = -1; for(std::vector<int>::iterator iter = v.begin(); iter != v.end(); ++iter) { if(*iter > x && *iter < y) { i = *iter; break; } } It's not only much more verbose, but the types need to be expressly written, and what's going on isn't immediately obvious from the function name. No, C++ will never win a beauty contest (I'd vote for D or Haskell personally), but it's great for expressibility, and the new C++11 changes make things more concise.
- kleiba 15y agoAgree completely. When you compare the new code to the C++98 one, it looks like a great improvement. But isn't that because C++98 simply sucked even more? As you say, when you compare it to languages like Haskell, even the new C++11 leaves something to be desired.
- cageface 15y agoThis almost looks like ruby. For a language as close to the metal as C++ I think that's pretty impressive.
- pyrtsa 15y agoI think the lambdas are the more useful the more complicated the function is. Simple asynchronous tasks might be one such example. This one, however, still is a pretty common use case, so introducing a function object type for it wouldn't hurt readability. Something like: template <typename A, typename B> struct between_ { A a; B b; between_(A const & a, B const & b) : a(a), b(b) {} template <typename T> bool operator()(T const & t) const { return a < t && t < b; } }; template <typename A, typename B> between_<A, B> between(A const & a, B const & b) { return between_<A, B>(a, b); } // ... auto i = find_if(begin(v), end(v), between(x, y)); Verbose? Admittedly. Except maybe for the point of use, i.e. the last line. Which, I think, helps a lot anyway.