Skip to content

Latest commit

 

History

History
163 lines (111 loc) · 4.39 KB

File metadata and controls

163 lines (111 loc) · 4.39 KB

Modern C++ (11\14\17)

C++17

TopicDescription
initalization listmore (…) Initalization List
inline variablesmore (…) inline variables
anonymous namespacesmore (…) anonymous namespaces
nested namespacesmore (…) nested namespaces
template decuctionmore (…) template deduction
initializer if-statementmore (…) initializer if-statement
structure unbindingmore (…) structure binding
string_viewmore (…) string_view
filesystemmore (…) filesystem
parallel algorithmsmore (…) Parallel Algorithms

Lambdas

TopicDescription
lambda recursivemore (…) lambda recursive
capture *this (c++17)more (…) *this (C++17)
lambdas as callbackmore (…) lambda as callback
monadsmore (…) Source Code
othersmore (…) Source Code

mutable

Mutable keyword can be used for change a value using a const function

source: https://stackoverflow.com/questions/105014/does-the-mutable-keyword-have-any-purpose-other-than-allowing-the-variable-to

#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;
}

Variadic Templates

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

Move semantics

Source Code

Features by standard

C++11 C++14 C++17