Skip to content

Instantly share code, notes, and snippets.

@herval
Created March 18, 2017 04:48
Show Gist options
  • Save herval/88b2a7341c99115783f624db84313d9a to your computer and use it in GitHub Desktop.
Save herval/88b2a7341c99115783f624db84313d9a to your computer and use it in GitHub Desktop.
C++11 syntax & stuff
#include <iostream>
// template (generics)
template<typename F>
auto &f(F &param) {
return param;
}
// TODO reflection
// TODO type casting/switching
namespace Test {
struct Foo {
int x;
std::string y;
};
}
// const = immutable
// & = reference (w/o & = copy)
int sum(const std::initializer_list<int> &list) {
// iterator
auto total = 0;
for (auto &e : list) {
total += e;
}
// lambda
auto functionFoo = [](int x, int y) { return x + y; };
std::cout << functionFoo(10, 20) << std::endl;
// closure
int x = 10;
auto generator = [x](int y) { return x * y; };
std::cout << generator(2) << std::endl;
std::cout << f("foo") << std::endl;
return total;
}
// this gets "inlined" in compile time!
constexpr int basicallyVal(int x, Test::Foo &foo) {
foo.y = "modified!";
if (x % 2 == 0) {
return x * 2;
}
return x;
}
int main() {
std::cout << "Hello, World!" << std::endl;
auto v = Test::Foo{
1,
"foo"
};
std::chrono::milliseconds(10);
auto foo = 123;
std::printf(
"hello %d - %d - %s\n",
sum({1, 2, 3, 4, 5, 6}),
basicallyVal(10, v),
v.y.c_str()
);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment