| Topic | Description |
|---|---|
| initalization list | more (…) Initalization List |
| inline variables | more (…) inline variables |
| anonymous namespaces | more (…) anonymous namespaces |
| nested namespaces | more (…) nested namespaces |
| template decuction | more (…) template deduction |
| initializer if-statement | more (…) initializer if-statement |
| structure unbinding | more (…) structure binding |
| string_view | more (…) string_view |
| filesystem | more (…) filesystem |
| parallel algorithms | more (…) Parallel Algorithms |
| Topic | Description |
|---|---|
| lambda recursive | more (…) lambda recursive |
| capture *this (c++17) | more (…) *this (C++17) |
| lambdas as callback | more (…) lambda as callback |
| monads | more (…) Source Code |
| others | more (…) Source Code |
Mutable keyword can be used for change a value using a const function
#include <iostream>
using namespace std;
class Test
{
public:
Test() = default;
//silly method just for demonstration prospose
void increment() const
{
Id++;
}
inline int get() const
{
return Id++;
}
private:
mutable unsigned int Id = 0;
};
int main()
{
Test tt = {};
tt.increment();
cout << tt.get() << endl;
//capture by value
int x = 1;
auto f = [=]() mutable { x = 42; };
f();
cout << x << " - " << endl;
return 0;
}
Example from: Modern C++ programming cookbook
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
template < typename ... Ts >
auto sum (Ts ... ts)
{
return (ts + ...);
}
template< typename R, typename ... Ts>
auto matches( const R& range, Ts ... ts)
{
return (std::count(std::begin(range), std::end(range), ts) + ... );
}
template <typename T, typename ... Ts>
bool within(T min, T max, Ts ... ts)
{
return ((min <= ts && ts <= max) && ...);
}
template<typename T, typename ... Ts>
void collapse_vectors(std::vector<T> &vec, Ts ... ts)
{
(vec.push_back(ts), ...);
}
template <typename T>
void printline(T t)
{
cout << t ;
}
int main ()
{
int the_sum { sum (1, 2, 3, 4, 5)};
cout << "Sum: " << the_sum << endl;
string a {"Hello"};
string b {" World "};
cout << sum ( a, b ) << endl;
vector<int> v { 1,2,3,4,5};
cout << "Nr. of finds = " << matches(v, 2,5) << endl;
cout << "is between = " << within(10, 20, 12, 15) << endl;
cout << "is between = " << within(10, 20, 45, 55) << endl;
std::vector<int> vv {1,2,3};
collapse_vectors( vv, 5,6,8);
for_each(vv.begin(), vv.end(), printline<int>);
}Another example: Source Code