Skip to content

Instantly share code, notes, and snippets.

@PIlin
PIlin / crtp.cpp
Created September 1, 2015 20:48
crtp.cpp
#include <iostream>
using namespace std;
template <typename T, typename U>
struct Base
{
void TestBase(void* ptr) //__attribute__ ((noinline))
{
U& ref = *reinterpret_cast<U*>(ptr);
static_cast<T*>(this)->Test(ref);
#include <iostream>
#include <type_traits>
template<class T> class HasTypeTag {
// This version doesn't work as intended in GCC
template<class C> static std::true_type test(char[sizeof(C::TypeTag)]);
// This version works in GCC, Clang and VS2013
//template<class C> static auto test(void*) -> decltype(C::TypeTag, std::true_type());
template<class C> static std::false_type test(...);
public:
template <int A> struct aligned;
template <> struct __declspec(align(1)) aligned < 1 > {};
template <> struct __declspec(align(2)) aligned < 2 > {};
template <> struct __declspec(align(4)) aligned < 4 > {};
template <> struct __declspec(align(8)) aligned < 8 > {};
template <> struct __declspec(align(16)) aligned < 16 > {};
template <> struct __declspec(align(32)) aligned < 32 > {};
template <int size, int align>
union aligned_type {