Skip to content

Instantly share code, notes, and snippets.

@remyroez
remyroez / SFINAE-safe_underlying_type.cpp
Last active November 2, 2016 07:09
SFINAE 安全な underlying_type
#include <iostream>
#include <type_traits>
template <typename T, typename = typename std::is_enum<T>::type>
struct safe_underlying_type { using type = T; };
template <typename T>
struct safe_underlying_type<T, std::true_type> { using type = std::underlying_type_t<T>; };
template <typename T>
@remyroez
remyroez / SFINAE-provides_bitwise_operators.cpp
Created November 2, 2016 07:07
SFINAE enum にビット演算を提供する(写経)
#include <type_traits>
#include <iostream>
template <typename T, typename = typename std::is_enum<T>::type>
struct safe_underlying_type { using type = T; };
template <typename T>
struct safe_underlying_type<T, std::true_type> { using type = std::underlying_type_t<T>; };
template <typename T>
@remyroez
remyroez / private-access.cpp
Created November 2, 2016 09:55
private メンバにアクセス(写経)
#include <iostream>
// impl ----------
template <typename T>
struct accessor {
static typename T::type value;
};
template <typename T>
@remyroez
remyroez / type_name.cpp
Created November 10, 2016 09:19
タイプ名のデマングル
#include <iostream>
#include <sstream>
#ifdef __GNUG__
#include <cstdlib>
#include <memory>
#include <cxxabi.h>
std::string demangle(const char *name) {
@remyroez
remyroez / logical-operator-type-traits.cpp
Last active November 12, 2016 05:55
Logical Operator Type Traits
#include <type_traits>
// impl ----------
#if (__cplusplus > 201402L)
using std::bool_constant;
using std::negation;
using std::conjunction;
using std::disjunction;
@remyroez
remyroez / force_cast.cpp
Created November 14, 2016 09:04
Force cast
#include <iostream>
template <typename T, typename U>
constexpr inline T &force_cast(U &u) { return (*reinterpret_cast<T *>(&u)); }
template <typename T, typename U>
constexpr inline const T &force_cast(const U &u) { return (*reinterpret_cast<const T *>(&u)); }
struct foo { int value = 123; };
@remyroez
remyroez / state-machine.cpp
Created November 16, 2016 07:50
State Machine
#include <iostream>
#include <functional>
#include <memory>
// impl ----------
struct state {
using pointer = std::shared_ptr<state>;
virtual pointer operator()() = 0;
};
@remyroez
remyroez / auto_constant.cpp
Created November 16, 2016 08:58
auto_constant
#include <iostream>
// impl ----------
template <auto V>
struct auto_constant {
static constexpr auto value = V;
using value_type = decltype(value);
using type = auto_constant;
@remyroez
remyroez / invoke.cpp
Last active November 17, 2016 04:11
invoke test
#include <iostream>
#include <functional>
#include <type_traits>
// impl ----------
template <typename T, typename F = decltype(+std::declval<T>())>
class fn {
public:
using type = std::remove_pointer_t<F>;
@remyroez
remyroez / non-virtual-destructor.cpp
Created November 17, 2016 05:01
基底クラスにおいてデストラクタが非仮想の場合、派生クラスのデストラクタが呼ばれる場合、呼ばれない場合
#include <iostream>
#include <memory>
class foo {
public:
foo() { std::cout << __func__ << std::endl; }
~foo() { std::cout << __func__ << std::endl; }
};
class bar : public foo {