2010-12-01から1ヶ月間の記事一覧

boost::mpl を使った再帰処理

mpl::lambda で再帰処理を行うメタ関数を定義してみたいと思います。 今回は再帰処理で、0〜n までの合計を求めてみたいと思います。 通常の関数定義だとこんな感じです。 int sum(int n){ if(0 >= n){ return n; } else{ return n + sum(n-1); } } さて、通…

template で式を書く

C++

template で式を定義し、実行時に展開を行うとかそんな感じです。 説明するよりも見たほうが早いと思うので、まずは使い方を。 // 処理を定義する。_1 は呼び出し時の引数 typedef plus<int_<2>, _1> plus_2_x; int n = plus_2_x()(3); // 2+ 3 を計算して返す BOOST</int_<2>…

構造体のメンバ変数を定数の値で取得したい その2

前回の続きです。 コメントにも頂きましたが、std::mt の年と月のデータは特殊で、そのまま渡しても正しい値として認識がされません。 ですので、変数に渡す場合は、差分を計算して、代入しなければなりません。 std::tm st; st.tm_year = 2010 - 1900; // 1…

boost::mpl を使用した twice の実装

[お題] The lambda Metafunction に書かれている、twice を mpl::lambda や mpl::apply を使用して定義してみる。 // 元のメタ関数 template <class F, class X> struct twice : apply1< typename mpl::lambda<F>::type , typename apply1< typename mpl::lambda<F>::type , X >::typ</f></f></class>…

if 文の遅延処理?

C++

ある値(関数の戻り値等)が true だった場合に呼ばれる関数をあらかじめ設定してしまうとかそんな感じ。 とりあえず、何も考えずに実装してみる。 #include <iostream> #include <boost/function.hpp> #include <boost/fusion/include/vector.hpp> #include <boost/fusion/include/at_c.hpp> namespace fusion = boost::fusion; typedef boost::function<bool()> if_</bool()></boost/fusion/include/at_c.hpp></boost/fusion/include/vector.hpp></boost/function.hpp></iostream>…

構造体のメンバ変数を定数の値で取得したい

C++

関連:Shand.DateTime v1.2 #include <iostream> #include <ctime> #include <boost/assert.hpp> #include <boost/multi_index/member.hpp> #include <boost/mpl/vector.hpp> #include <boost/mpl/at.hpp> #include <boost/mpl/for_each.hpp> namespace multi_index = boost::multi_index; namespace mpl = boost::mpl; typede…</boost/mpl/for_each.hpp></boost/mpl/at.hpp></boost/mpl/vector.hpp></boost/multi_index/member.hpp></boost/assert.hpp></ctime></iostream>

boost::mpl::map から任意の key を持つ pair を抽出

[お題] boost::mpl::map から任意の key を持つ pair を Sequence として抽出したい。 // key が char 型の pair を抽出したい!! mpl::map< mpl::pair<char, int>, mpl::pair<int, float>, mpl::pair<float, char>, mpl::pair<char, float>, mpl::pair<int, int> > #include <boost/mpl/assert.hpp> #include <boost/mpl/equal.hpp> #include </boost/mpl/equal.hpp></boost/mpl/assert.hpp></int,></char,></float,></int,></char,>

boost::mpl::map で、second を抽出

[お題] boost::mpl::map から second の値を Sequence として抽出したい。 #include <boost/type_traits/is_same.hpp> #include <boost/mpl/assert.hpp> #include <boost/mpl/equal.hpp> #include <boost/mpl/map.hpp> #include <boost/mpl/transform_view.hpp> #include <boost/mpl/vector.hpp> #include <boost/mpl/at.hpp> namespace mpl…</boost/mpl/at.hpp></boost/mpl/vector.hpp></boost/mpl/transform_view.hpp></boost/mpl/map.hpp></boost/mpl/equal.hpp></boost/mpl/assert.hpp></boost/type_traits/is_same.hpp>

各 vec 型に対する次元数の取得方法

関連:クラスに持たせる定数について http://d.hatena.ne.jp/gintenlabo/20101214/1292329739 現状の個人的なまとめ。 vec 型の次元数を取得するメタ関数 dimension の実装手段をいくつか。 使い方はこんな感じ。 struct my_vec{ float x, y, z; }; BOOST_ST…

boost::mpl::sort で任意のソート

[お題] boost::mpl::sort を任意の値でソートしたい #include <boost/mpl/vector.hpp> #include <boost/mpl/sort.hpp> #include <boost/mpl/equal.hpp> #include <boost/mpl/reverse.hpp> #include <boost/mpl/greater.hpp> namespace mpl = boost::mpl; typedef mpl::vector<float, double, char, int> source; typedef mpl::vector</float,></boost/mpl/greater.hpp></boost/mpl/reverse.hpp></boost/mpl/equal.hpp></boost/mpl/sort.hpp></boost/mpl/vector.hpp>

エラー処理のメモ

C++

[例外] std::vector<T>::at(n); [assert] std::vector<T>::operator [n]; pstade::oven::at(n);</t></t>

定数の定義

C++

boost::fusion を使っててふと思ったんですが。 typedef std::size_t dimension_type; struct my_vec{ static const dimension_type dimension = 3; float x, y, z; }; BOOST_STATIC_ASSERT( my_vec::dimension == 3 ); こうやって定義するよりも……。 templa…

one-phase construction

C++

書くネタが無いので、他の記事に便乗するでゲソ! 関連:One-Phase Construction 入門 〜 Constructor run once つまりこういうことでゲソか? #include <boost/noncopyable.hpp> struct vec : public boost::noncopyable{ vec() : x(0.0f), y(0.0f), z(0.0f){} vec(float _x, float</boost/noncopyable.hpp>…

定数の has_xxx

C++

template<int N> struct sfinae_helper{ typedef void type; }; template<typename T, typename U = void> struct has_dimension_impl{ static const bool value = false; }; template<typename T> struct has_dimension_impl<T, typename sfinae_helper<T::dimension>::type>{ static const bool value = true; }; templat…</t,></typename></typename></int>

お詫び

先日、知り合いと Twitter でやり取りをしていて初めて知ったのですが…… フォローしてないとリプライされてもタイムラインに表示されないんですねいやーてっきり表示されるとばかり思っていました。 今までわたしにリプライされた方がいたら気づかなくてすみ…

新規ファイル作成時に使用するテンプレートファイルを指定して作成

auto-insert では、ファイル名(拡張子等)以外で使用するテンプレートファイルの切り替えが出来なかったので、ちょっと改造してみました。 元となった elisp はこちらです。 http://www.02.246.ne.jp/~torutk/cxx/emacs/mode_extension.html [before] ;; 〜…

新規ファイルのテンプレート

VisualStudio で新規ファイルのテンプレートをつくろうと思ったのですが VC には作成する機能が無いみたいです。 (正確にいえばエクスポートが出来ないみたい) しょうがないので、VisualStudio の外部ツールで Emacs を呼び出して、autoinsert の機能を使…