Skip to content

Instantly share code, notes, and snippets.

View odeblic's full-sized avatar

Olivier de BLIC odeblic

View GitHub Profile
@odeblic
odeblic / move.cpp
Last active July 27, 2017 11:28
Several use cases around move semantic
#include <iostream>
#include <mutex>
struct Object
{
Object(int id) : id(id)
{
std::cout << '[' << id << "] constructor" << std::endl;
}
@odeblic
odeblic / call_once.cpp
Last active July 27, 2017 11:27
Simple example for std::call_once
#include <iostream>
#include <mutex>
#include <functional>
struct Object
{
Object(int n) : n(n) {}
void init() { std::call_once(initialized, [this] { std::cout << "init with n=" << n << std::endl; }); }
@odeblic
odeblic / except.cpp
Last active July 27, 2017 11:26
Exception handling, enabled or not
#include <iostream>
#include <exception>
#include <cstdlib>
void f() noexcept(false)
{
std::cout << "f()" << std::endl;
throw('F');
}
@odeblic
odeblic / metaprogramming.cpp
Last active July 27, 2017 11:25
Metaprogramming with compile-time computed fibonacci and factorial functions
#include <iostream>
template <unsigned int N>
struct factorial
{
enum { value = N * factorial<N - 1>::value };
};
template <>
struct factorial<0>
@odeblic
odeblic / variadic_inheritance.cpp
Last active July 27, 2017 11:23
Inherit several bases by using variadic templates
#include <iostream>
#include <type_traits>
#include <typeinfo>
template <typename BASE, typename ... BASES>
struct MultiDerived : BASE, MultiDerived<BASES> ...
{
void EveryAction()
{
BASE::Action();
@odeblic
odeblic / smart_pointers.cpp
Last active July 28, 2017 03:16
Why we should be careful when deciding to use the factory function std::make_shared
#include <iostream>
#include <memory>
#include <list>
struct Heavy
{
char buffer[1024 * 1024 * 8];
};
std::list<std::weak_ptr<Heavy>> container;
@odeblic
odeblic / exchange.cpp
Created July 28, 2017 04:34
Draft of exchange simulator
#include <iostream>
#include <mutex>
#include <functional>
class Price
{
std::uint64_t value;
std::ratio<1, 100> value;
};
@odeblic
odeblic / sfinae.cpp
Created July 28, 2017 09:04
SFINAE in practice
#include <iostream>
struct Object
{
typedef int type;
static void action()
{
std::cout << "Object::action()" << std::endl;
}
@odeblic
odeblic / size_vtable.cpp
Created July 28, 2017 10:44
Size of classes with and without a vtable
#include <iostream>
struct A
{
};
struct B : A
{
};
@odeblic
odeblic / diamond.cpp
Last active July 28, 2017 11:15
Consequence of diamond inheritance
#include <iostream>
struct X
{
protected:
int i;
};
struct A : X
{