This is part of my weekly C++ posts based on the daily C++ tips I make at my work. I strongly recommend this practice. If you dont have it in your company start it.
1. The first rule about performance
class Base
{
public:
Base(int x, int y) : x_(x), y_(y) {}
public:
int x_;
int y_;
};
class Derived : public Base
{
using Base::Base;
};
class Boo {
public:
auto getValueMultiplier() {
return [=](int multiplier) {
return value * multiplier;
};
}
private:
int value = 5;
};
auto lambda1 = []() {};
auto lambda2 = []() {};
std::cout << typeid(decltype(lambda1)).name() << '\n';
std::cout << typeid(decltype(lambda2)).name() << '\n';
auto make_f = [](int multiplyer ) {
return [multiplyer](const int& i) {
return i * multiplyer;
};
};
auto Funcs = std::vector<decltype(make_f(0))>();
Funcs.push_back(make_f(1));
Funcs.push_back(make_f(2));
if (thing.x == 1 || thing.x == 2 || thing.x == 3)you can write:
template<typename U, typename ... T>
bool one_of(U&& u, T && ... t)
{
return ( (u == t) || ... );
}
Syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express.For the first syntactic sugar we have to go back in time. In C a[i] is syntactically equivalent to *(a + i) which allows us to write this:
int a[5];but also this:
a[2] = 1;
3[a] = 5;Which more than perfectly illustrates what a syntactic sugar is.
std::vector<int> v = {0, 1, 2, 3, 4, 5};
for (const int& i : v) // access by const reference
std::cout << i << ' ';