Skip to content

Instantly share code, notes, and snippets.

View nekko1119's full-sized avatar
🍙
nano!

nekko1119 nekko1119

🍙
nano!
View GitHub Profile
@nekko1119
nekko1119 / enable_shared_from_this_like.cpp
Last active December 19, 2015 12:29
enable_shared_from_thisを継承したクラスのshared_ptrが作成された時に、enable_shared_from_thisのprivateデータメンバであるweak_ptrがどのようにしてthisで初期化されているのか、イメージを簡略的に書いてみた。スマートポインタとしての機能は無いです。
#include <iostream>
#include <cassert>
using std::cout;
template <class T>
class weak_ptr;
template <class T>
class enable_shared_from_this;
@nekko1119
nekko1119 / value_type_oridentity.cpp
Created July 13, 2013 06:15
Tがvalue_typeを持っていればvalue_typeを、持っていなければTを返すメタ関数。 参考https://twitter.com/kester44/status/348712647203618816
#include <type_traits>
template <bool C, class T, class F>
struct eval_if_c;
template <class T, class F>
struct eval_if_c<true, T, F>
{
typedef typename T::type type;
};
@nekko1119
nekko1119 / expression.hpp
Created July 30, 2013 13:00
boost見ながら式テンプレートを用いた数学のベクトル書いてみた。
//ファンクタを適用する式
#ifndef NEK_MATH_EXPRESSION_HPP
#define NEK_MATH_EXPRESSION_HPP
#include <type_traits>//is_same, declval, ...
namespace nek
{
namespace math
{
@nekko1119
nekko1119 / erlang_distribution_simulation.cpp
Created July 30, 2013 12:51
授業の課題で、アーラン分布における待ち行列のシミュレーションを書くプログラム。途中で力尽きた。
#include <algorithm>//copy
#include <cassert>//assert
#include <iostream>//cout
#include <iterator>//iterator
#include <limits>//numeric
#include <memory>//shared_ptr
#include <numeric>//accumelate
#include <random>//mt19937, distribution
#include <sstream>//stringstream;
#include <type_traits>//is_arithmetic
@nekko1119
nekko1119 / factorial.cpp
Last active December 20, 2015 12:39
C++のメタプログラミングにおけるhello worldポジションこと、階乗を求めるfactorial関数、数値でのメタ関数としてはよく見るけど、型のメタ関数として書いたことがなかったので試しに書いてみた
#include <boost/mpl/int.hpp>
#include <boost/mpl/eval_if.hpp>
#include <boost/mpl/multiplies.hpp>
#include <boost/mpl/equal_to.hpp>
using namespace boost;
template <class I>
struct factorial
: mpl::eval_if
<
@nekko1119
nekko1119 / cast.cpp
Last active December 22, 2015 04:28
良い感じにcast呼び分けてくれるcast考えてみた。絶対誰かやってる。
#include <type_traits>
#include <utility>
#include <iostream>
struct static_cast_
{
template <class To, class From>
static To cast(From&& val)
{
@nekko1119
nekko1119 / cast2.cpp
Last active December 22, 2015 07:09
良い感じに分けるcastのSFINAE版
#include <iostream>
#include <type_traits>
#include <utility>
template <class To, class From>
struct needs_to_static_cast
: public std::integral_constant<bool,
std::is_convertible<typename std::remove_reference<From>::type, To>::value>
{};
@nekko1119
nekko1119 / index_tuple.cpp
Last active December 23, 2015 08:29
引数群を受け取る練習。index tuple idiomを使う練習。VC++12.0で書きました。gccで動かすには`_Make_arg_idx`を`_Build_index_tuple`に、`_Arg_idx`を`_Index_tuple`に置き換えば動くと思います多分(確認していない)
#include <tuple>
#include <list>
#include <iostream>
using namespace std;
class hoge
{
public:
template <class... T1, class... T2>
hoge(tuple<T1...> t1, tuple<T2...> t2)
@nekko1119
nekko1119 / async.cpp
Last active December 23, 2015 10:19
処理を別スレッドで行い、メインスレッドは進捗をパーセント表示する。大きい数にも対応したくてBoost.Multiprecisionを使ったせいでコードが複雑になった感。変数iとnの読み書きがスレッドセーブじゃない気がする…
#include <future>
#include <iostream>
#include <chrono>
#include <thread>
#include <string>
#include <algorithm>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
boost::multiprecision::cpp_int simple(boost::multiprecision::cpp_int& n, int& i)
@nekko1119
nekko1119 / serialization_nodefault_constructor
Created November 23, 2013 14:14
Boost.Serializationの使い方を理解するために書いたサンプル
#include <iostream>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/nvp.hpp>
class Hoge
{
public:
Hoge(int d, double& r) : data_(d), ref_(r) {}
int data() const { return data_; }
double const& ref() const { return ref_; }