Skip to content

Instantly share code, notes, and snippets.

View odeblic's full-sized avatar

Olivier de BLIC odeblic

View GitHub Profile
@odeblic
odeblic / shared_mutex.cpp
Last active July 27, 2017 11:34
Shared mutex discovery
#include <mutex>
#include <shared_mutex>
#include <iostream>
template <typename M>
void test(M& mutex)
{
bool exclusive = false;
bool shared = false;
@odeblic
odeblic / variadic_templates.cpp
Last active July 27, 2017 11:25
Simple example using variadic templates
#include <string>
#include <iostream>
#include <typeinfo>
template <typename T>
void f(T t)
{
std::cout << typeid(t).name() << ":" << t << std::endl;
}
@odeblic
odeblic / types.cpp
Last active July 27, 2017 11:34
Study of primitive built-in types
#include <iostream>
#include <typeinfo>
#define PRINT_EXPR(T) do { std::cout << "parameter: " << #T << "\t\t\t\ttype: " << typeid(T).name() << std::endl; } while(false);
#define PRINT_TYPE(T) do { T val; std::cout << "parameter: " << #T << "\t\t\t\ttype: " << typeid(val).name() << std::endl; } while(false);
template <typename T>
void print(T t)
{
std::cout << "type: " << typeid(T).name() << " value: " << t << std::endl;
@odeblic
odeblic / if_scope.cpp
Last active July 27, 2017 11:32
Scope of variable within an if statement
#include <iostream>
int main()
{
int cond = 1;
if (int var = cond)
{
std::cout << "if: var=" << var << "\n";
}
@odeblic
odeblic / access.cpp
Last active July 27, 2017 11:31
Legal member access violation with polymorphism
#include <iostream>
class Base
{
public:
virtual void action() { std::cout << "Base::action()\n"; }
};
class Derived : public Base
{
@odeblic
odeblic / new.cpp
Last active July 27, 2017 11:31
Different versions of new operator
#include <iostream>
struct Object
{
Object(int n = 0) : n(n)
{ std::cout << '[' << n << ']' << " Object::Object()" << std::endl; }
~Object()
{ std::cout << '[' << n << ']' << " Object::~Object()" << std::endl; }
@odeblic
odeblic / lambdas.cpp
Last active July 27, 2017 11:30
How capture work with lambda functions
#include <iostream>
int main(void)
{
int a = 1001;
long double b = 3.1415926;
char c = 'A';
std::string d = "I like C";
std::cout << "a: " << a << std::endl;
@odeblic
odeblic / locks.cpp
Last active July 27, 2017 11:30
Deadlock avoidance with std::lock
#include <iostream>
#include <mutex>
struct Lock
{
Lock(int id) : id(id) {}
void lock() { std::cout << "lock() from " << id << std::endl; }
bool try_lock() { std::cout << "try_lock() from " << id << std::endl; return true; }
void unlock() { std::cout << "unlock() from " << id << std::endl; }
int id;
@odeblic
odeblic / async.cpp
Last active July 27, 2017 11:29
Asynchronous features (async, promises, futures)
#include <iostream>
#include <future>
#include <thread>
std::mutex mu;
template <typename T>
void print(std::string message, T data)
{
std::lock_guard<std::mutex> lg(mu);
@odeblic
odeblic / callable.cpp
Last active July 27, 2017 11:29
Construction of callable objects
#include <iostream>
#include <thread>
#include <list>
struct Object
{
void method(int n) {}
void operator()(int n) {}
};