Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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 / detection-idiom.cpp
Last active November 11, 2016 08:35
Detection Idiom
#include <type_traits>
// impl ----------
#if (__cplusplus > 201402L)
using std::void_t;
#else
@remyroez
remyroez / SFINAE-concept.cpp
Last active November 21, 2016 06:58
SFINAE コンセプト
#include <iostream>
#include <type_traits>
#if (__cplusplus > 201402L)
using std::void_t;
using std::bool_constant;
using std::negation;
using std::conjunction;
using std::conjunction_v;
@remyroez
remyroez / SFINAE-has-value.cpp
Created November 1, 2016 08:19
SFINAE 特定のメンバ変数を持っているかどうかの trait
#include <iostream>
#include <type_traits>
struct has_value_impl {
template <class T, class U = decltype(T::value)>
constexpr static std::is_same<U, int> test_variable(int);
template <typename...>
constexpr static std::false_type test_variable(...);
@remyroez
remyroez / utf32-string-stream.cpp
Created November 1, 2016 00:32
UTF32 版 stringstream
#include <iostream>
#include <sstream>
#include <string>
#include <locale>
#include <codecvt>
using w32stringstream = std::basic_stringstream<char32_t>;
int main()
{