Skip to content

Instantly share code, notes, and snippets.

#include <stdexcept>
#include <utility>
#include <cassert>
#if 0
#define NORETURN [[noreturn]]
#else
#define NORETURN
#endif
#include <memory>
#include <string>
struct hoge {
explicit hoge(std::string name, int value);
~hoge();
hoge(hoge&&) = delete;
void operator=(hoge&&) = delete;
@gintenlabo
gintenlabo / dlfcn.hpp
Last active November 10, 2022 00:23
dlopen and C++ classes
#ifndef INCLUDED_DLFCN_HPP_
#define INCLUDED_DLFCN_HPP_
#include <dlfcn.h>
#include <cassert>
#include <stdexcept>
namespace dlfcn {
struct dlfcn_error : std::runtime_error {
@gintenlabo
gintenlabo / hoge.cc
Created June 8, 2013 10:06
pImpl idiom in C++11
#include "hoge.hpp"
#include <iostream>
struct Hoge::Impl {
int member;
};
void Hoge::ImplDisposer::operator()(Hoge::Impl* p) const {
delete p;
@gintenlabo
gintenlabo / unique_by.cc
Created June 6, 2013 06:39
Algorithm unique_by
#include <utility>
template<class FwdIter, class Eq, class Merge>
inline FwdIter unique_by(FwdIter first, FwdIter last, Eq eq, Merge merge) {
if (first == last) {
return first;
}
FwdIter cur = first;
for (++first; first != last; ++first) {
if (eq(*cur, *first)) {
@gintenlabo
gintenlabo / define_function_macro.cc
Last active December 16, 2015 05:09
関数を定義するマクロ. noexcept 性も正しく推論される. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3207.htmhttp://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3582.html が採択されれば,こんな面倒なことはしなくていいんですけどね.
namespace etude {
namespace functions_ {
template<class T>
constexpr T copy_(T x) noexcept
{
return x;
}
}
using namespace functions_;
@gintenlabo
gintenlabo / is_copyable.cc
Last active December 16, 2015 04:39
is_callable の実装 厳密には INVOKE を使うべき 気まぐれで関数形式にしてあるけど struct でも別に良い
#include <type_traits>
template<class T>
struct type {};
#define TYPE_OF(...) (type<decltype(__VA_ARGS__)>{})
struct is_callable_t
{
template<class F, class... Args,
@gintenlabo
gintenlabo / decay_copy.cc
Last active December 16, 2015 04:39
decay_copy は標準に必要だと思うんだ
#include <type_traits>
#include <utility>
// checking whether U{std::declval<T>()} is well-formed,
// where U is typename std::decay<T>::type
template<class T>
struct is_decay_copyable
: std::is_constructible<typename std::decay<T>::type, T>::type {};
// typename decay_if_possible<T>::type is defined as typename std::decay<T>::type,
import Data.List (minimum)
import Data.Function (on)
import Data.Ratio ((%))
-- return minimum element compared by given function
-- (return value used by compare, too)
-- another implementation (more generic ver.)
newtype CompOnSnd a b = CompOnSnd { fromCompOnSnd :: (a, b) }
@gintenlabo
gintenlabo / DataFactory.hs
Last active December 15, 2015 15:58
値の候補からデータを生成するような Factory
{-# LANGUAGE RankNTypes #-}
import System.Random
import Data.Functor
import Control.Monad.Identity
import Control.Monad.Trans
import Control.Monad.Trans.List
type Factory m r = Monad m => (forall a. [a] -> m a) -> m r