3 ms·
This—also, C++11 is an amazing improvement over C++03, and C++14 looks like another step in the right direction. For a small example, a friend talked me into i
by LnxPrgr3 13y ago
This—also, C++11 is an amazing improvement over C++03, and C++14 looks like another step in the right direction.
For a small example, a friend talked me into implementing a few Scala collection operations in C++11. Here's the result: https://gist.github.com/LnxPrgr3/6547131 https://gist.github.com/LnxPrgr3/6547131
Usage looks like this:
const collection<vector<string>> names = {"Daniel", "Chris", "Joseph"};
return names.reduce_left<string>([](const string &total, const string &value) {
return total + ", " + value;
}) == "Daniel, Chris, Joseph";
// ...
const collection<vector<string>> strs = {"1", "2", "3", "4", "5"};
auto rv = strs.map<collection<vector<int>>>([](const string &x) {
return atoi(x.c_str());
});
return rv.size() == 5 && rv[0] == 1 && rv[1] == 2 && rv[2] == 3 && rv[3] == 4 && rv[4] == 5;
This is from a collection of tests, which looks like this:
struct test {
const char *name;
function<bool ()> fn;
};
const vector<test> tests = {
{"Foldleft", []{ /* .... */}},
{"reduceLeft on empty throws", []{ /* ... */ }},
// ...
};
for(auto &test: tests) {
cout << test.name << ": " << flush << (test.fn() ? "PASS" : "FAIL") << endl;
}
I would've never considered something like this useful in C++03—callers would have to create named functor classes just to do some trivial one-line operation, this vector of tests would've had to have been a series of push_back calls, and iterating over it would've been quite a bit uglier.
This isn't at all an exhaustive list, but these are the biggest things that come to mind when I think of what C++11 gains over C++03.