Procuring digital preservation CAN be quick and painless with our new dynamic...
Fun with Lambdas: C++14 Style (part 2)
1. Sumant Tambe, Ph.D.
Microsoft Visual C++ MVP
Senior Software Research Engineer
Real-Time Innovations, Inc.
@sutambe
SFBay Association of C/C++ Users
April 9, 2014
7. class ##### {
float a;
public:
Foo(float x) {
a = x;
}
bool operator () (float b) const {
return std::abs(a) < std::abs(b);
}
};
float array[5] = { 0, 1, 2, 3, 4 };
float a = 3;
auto f = #####(a);
for(float item : array)
std::cout << std::boolalpha << f(item);
8. class ##### {
float a;
public:
Foo(float x) {
a = x;
}
bool operator () (float b) const {
return std::abs(a) < std::abs(b);
}
};
float array[5] = { 0, 1, 2, 3, 4 };
float a = 3;
auto f = #####(a);
auto f = [a](float b) { return std::abs(a) < std::abs(b) };
for(float item : array)
std::cout << std::boolalpha << f(item);
9. » Anonymous functions
» Written exactly in the place where it's needed
» Can access the variables available in the
enclosing scope (closure)
» May maintain state (mutable or const)
» Can be passed to a function
» Can be returned from a function
» Deduce return type automatically
» Accept generic parameter types (only in C++14)
[a](auto b) { return std::abs(a) < std::abs(b) };
10. » Associative containers and lambdas
» Recursive Lambdas
» Composable list manipulation
» Scope Exit Idiom
» Overloaded Lambdas
» Type Switch (simple pattern matching)
» Converting shared_ptr between boost and std
» In-place parameter pack expansion
» Memoization
11. » Associative containers: set, map, multiset, multimap
˃ Use comparators
˃ Example, std::set<int, std::less<int>>
» Can comparators be a lambda?
» std::set<int, ???>
» Use std::function as the comparator type
std::set<int, std::function<bool(int, int)>>
numbers([](int i, int j) { return i < j; });
» Small-closure optimization may kick in
˃ Check you compiler manual
17. » Cpplinq http://cpplinq.codeplex.com/
» Narl (Not another range library) https://github.com/essennell/narl
» boost.range http://www.boost.org/doc/libs/1_54_0/libs/range/
» Linq http://pfultz2.github.io/Linq/
» boolinq http://code.google.com/p/boolinq/
» lx++ (part of Native-RX) https://rx.codeplex.com/
» Linq++ https://github.com/hjiang/linqxx/
» oven http://p-stade.sourceforge.net/oven/
» cpplinq (same name but
unrelated) http://code.google.com/p/cpplinq/
» linq https://bitbucket.org/ronag/cppextras/src/master/linq/linq.hp
p
18. » Language for Embedded
Query and Traversal
» Tree Traversal
˃ For XML data-binding code
generators
˃ Visitors
» Think XPath queries
embedded in C++
˃ Typed (not string encoded)
» Generic Programming
˃ Expression Templates
˃ Meta-programming
˃ C++ Concepts (no longer in C++11)
http://www.dre.vanderbilt.edu/LEESA/
25. » Boost and std::shared_ptr manage memory automatically
» They both use their own reference counters
» What happens when you convert from boost to std shared_ptr
or vice versa
template <typename T>
boost::shared_ptr<T>
make_shared_ptr(std::shared_ptr<T> ptr)
{
return boost::shared_ptr<T>(ptr.get());
}
» Hint: They both support deleters
˃ malloc = allocator
˃ free = deleter
std::shared_ptr<char> buf((char *)malloc(10), free);