Apply-related code
This commit is contained in:
parent
04f920ac2d
commit
74318f3bda
2 changed files with 35 additions and 0 deletions
5
apply/Makefile
Normal file
5
apply/Makefile
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
# Only define if needed:
|
||||||
|
# TARGET = main.elf
|
||||||
|
# SRCS = main.cc
|
||||||
|
|
||||||
|
include ../config.mk
|
30
apply/main.cc
Normal file
30
apply/main.cc
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
#include <algorithm>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
// This code will kill your mood, steal your cat and invade russia
|
||||||
|
|
||||||
|
template <typename F>
|
||||||
|
auto apply(const F &f) -> decltype(f()) {
|
||||||
|
return f();
|
||||||
|
}
|
||||||
|
|
||||||
|
class Foo {
|
||||||
|
int val;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Foo() = default;
|
||||||
|
|
||||||
|
Foo(const int &x)
|
||||||
|
: val(x) {}
|
||||||
|
|
||||||
|
Foo operator()() const { return Foo(2 * val); }
|
||||||
|
|
||||||
|
operator int() const { return val; }
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::vector<Foo> v = {1, 2, 3, 4, 5};
|
||||||
|
std::vector<Foo> b;
|
||||||
|
|
||||||
|
std::transform(v.begin(), v.end(), std::back_inserter(b), apply<Foo>);
|
||||||
|
}
|
Loading…
Reference in a new issue