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

hax_xxx その2

関連:has_xxx decltype を使わなくても一応、出来るみたいなのでやってみました。 // T::value が特定できない場合は、 false ではなくコンパイルエラーに・・・ /* template<typename T> struct has_value{ typedef char yes; typedef struct{ char c[2]; } no; template<typename U> </typename></typename>…

boost::multi_index::multi_index_container

#include <iostream> #include <string> #include <boost/multi_index_container.hpp> #include <boost/multi_index/random_access_index.hpp> #include <boost/multi_index/ordered_index.hpp> #include <boost/multi_index/mem_fun.hpp> typedef boost::multi_index::multi_index_container< std::string, boost::multi_i…</boost/multi_index/mem_fun.hpp></boost/multi_index/ordered_index.hpp></boost/multi_index/random_access_index.hpp></boost/multi_index_container.hpp></string></iostream>

メンバ変数のアドレスを template に渡す

C++

#include <iostream> template< typename class_t, typename type, type class_t::*member_ptr > struct member{ typedef type result_t; result_t& operator ()(class_t& rhs) const{ return rhs.*member_ptr; } }; struct vec_t{ float x, y, z; }; member<vec_t, float, &vec_t::x> at_x; mem</vec_t,></iostream>…

メンバ変数のアドレスの取得と呼び出し

C++

#include <iostream> #include <assert.h> struct vec_t{ float x, y, z; }; int main(){ float vec_t::*x_member = &vec_t::x; vec_t vec; // 同じ場所をさしている assert(&(vec.x) == &(vec.*x_member)); vec.*x_member = 10; std::cout << vec.x << std::endl; return 0; } [</assert.h></iostream>…

std::set と std::multiset

C++

#include <iostream> #include <set> #include <pstade/oven/identities.hpp> #include <pstade/oven/io.hpp> int main(){ using pstade::oven::identities; std::set<std::string> strings; strings.insert("crossing"); strings.insert("days"); strings.insert("black"); strings.insert("very"); strings.insert("pie")…</std::string></pstade/oven/io.hpp></pstade/oven/identities.hpp></set></iostream>

使用コンパイラのチェック

C++

#include <iostream> #if defined (_MSC_VER) # if (_MSC_VER == 1600) # define COMPILER_NAME "vc10" # # elif (_MSC_VER == 1500) # define COMPILER_NAME "vc9.0" # # elif (_MSC_VER == 1400) # define COMPILER_NAME "vc8.0" # # else # define COMPILER_NAME "u</iostream>…

複数の msvc のバージョンでコンパイル

msvc-8.0 や msvc-9.0 等を一緒にコンパイルしたい。 とりあえず、先に結論だけ。 [build.bat] set BOOST_BUILD_PATH=/boost/boost_1_44_0/tools/build/v2 call "C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\vsvars32.bat" bjam toolset=msvc…

bjam を使用してコンパイル

ひとまず、手元の環境だとこれでコンパイルが出来たのでちょっとまとめ。 今回は、gcc を使用して、コンパイルを行います。 ☆事前にやっておくこと とりあえず、コマンドから bjam を呼び出せれば問題ないと思います。 (結構前に設定したからよく覚えてない…

boost::fusion::at と boost::fusion::at_c の違い

#include <assert.h> #include <boost/fusion/include/vector.hpp> #include <boost/fusion/include/at.hpp> int main(){ namespace fusion = boost::fusion; namespace mpl = boost::mpl; fusion::vector<float, float, float> v; // at() は、mpl::int_ で渡して、at_c() は、数値を直接渡す assert( fusion::at<mpl::int_<0> >(v) == fus…</mpl::int_<0></float,></boost/fusion/include/at.hpp></boost/fusion/include/vector.hpp></assert.h>

BOOST_TYPEOF(exp)

#include <boost/typeof/typeof.hpp> #include <boost/type_traits/is_same.hpp> #include <boost/static_assert.hpp> BOOST_STATIC_ASSERT(( boost::is_same<BOOST_TYPEOF(10), int>::value )); BOOST_STATIC_ASSERT(( boost::is_same<BOOST_TYPEOF(0.0f), float>::value )); BOOST_STATIC_ASSERT(( boost::is_same</boost_typeof(0.0f),></boost_typeof(10),></boost/static_assert.hpp></boost/type_traits/is_same.hpp></boost/typeof/typeof.hpp>

戻り値で SFINAE

#include <boost/utility/enable_if.hpp> #include <boost/type_traits/is_integral.hpp> template<typename T> void func(T, typename boost::enable_if<boost::is_integral<T> >::type* = 0 ){} // 戻り値型は、enable_if の第二引数 template<typename T> typename boost::enable_if<boost::is_integral<T>, void>::type func2(T){}…</boost::is_integral<t></typename></boost::is_integral<t></typename></boost/type_traits/is_integral.hpp></boost/utility/enable_if.hpp>

ベクトル型いろいろ

#include <iostream> #include <boost/tuple/tuple.hpp> #include <boost/array.hpp> #include <boost/polygon/polygon.hpp> #include <boost/numeric/ublas/vector.hpp> #include <NxPhysicsSDK.h> #include <d3dx9.h> #include <opencv/cv.h> struct my_vec_type{ float x, y, z; }; int main(){ // ユーザー定義 my_vec_type m…</opencv/cv.h></d3dx9.h></nxphysicssdk.h></boost/numeric/ublas/vector.hpp></boost/polygon/polygon.hpp></boost/array.hpp></boost/tuple/tuple.hpp></iostream>

std::valarray

C++

#include <iostream> #include <valarray> #include <boost/range/algorithm/for_each.hpp> #include <boost/range/adaptor/reversed.hpp> int main(){ std::valarray<float> array(100); std::valarray<float> result(100); // 代入したり演算したり array = 0.0f; array = array + 3.0f; // 関数の引数として渡せたり result = std::sin(array);…</float></float></boost/range/adaptor/reversed.hpp></boost/range/algorithm/for_each.hpp></valarray></iostream>

has_xxx

template< typename T > struct has_hoge{ typedef char yes; typedef struct{ char c[2]; } no; template<typename T> static yes check(T*, typename T::hoge* = 0); template<typename T> static no check(...); static const bool value = sizeof( check<T>(NULL) ) == sizeof(yes);</t></typename></typename>…

Boost #3 勉強会

関連:Boost.勉強会 #3 関西を開催しました 関西と言うことで、自分は参加できませんでしたが、今週の土日に boost 勉強会があったみたいですね。 関東なら行きたかったです…。 例によって、id:faith_and_brave さんがまとめて下さったのでざっくりと斜め読…

PImpl

PImpl イディオムは、ヘッダー内での include 数を減らしたい場合や実装を隠蔽する時に使用されるイディオムです。 id:melpon さんの C++ でのビルド時間を短縮するいくつかの方法 にチラッと出てきたアレです。 便乗ヒャッホイ そういえばあんまり使ったこ…

pstade::oven::single(x)

#include <iostream> #include <string> #include <pstade/oven/single.hpp> #include <pstade/oven/io.hpp> #include <pstade/oven/transformed.hpp> int main(){ namespace oven = pstade::oven; boost::equal(oven::single('F'), std::string("F")); std::cout << oven::single(10) << std::endl; // 1次元配列を2次元っぽく int a…</pstade/oven/transformed.hpp></pstade/oven/io.hpp></pstade/oven/single.hpp></string></iostream>

pstade::oven::dropped(n)

#include <iostream> #include <string> #include <pstade/oven/dropped.hpp> #include <pstade/oven/io.hpp> int main(){ namespace oven = pstade::oven; int array[] = {0, 1, 2, 3, 4, 5, 6, 7}; std::cout << (array|oven::dropped(3)) << std::endl; return 0; } [出力] {3,4,5,6,7} pstade::oven::dropped(n) は、range</pstade/oven/io.hpp></pstade/oven/dropped.hpp></string></iostream>…

7並べ 〜その4〜 トランプデータの生成

すでにぐだぐだになりつつある気がするけどまだがんばるお。 そんな訳でトランプのデータの生成を行ないます。 使うのは、昨日作った combination です。 こいつを使用して、絵柄と数字の組み合わせを列挙していきます。 ( 0w0) ウェーイ #include <iostream> #includ</iostream>…

pstade::oven::offset(n, m)

#include <iostream> #include <pstade/oven/offset.hpp> #include <pstade/oven/io.hpp> int main(){ namespace oven = pstade::oven; int array[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; std::cout << (array|oven::offset(2, -1)) << std::endl; return 0; } [出力] {2,3,4,5,6,7,8} pstade::oven::offset(n, m) は、</pstade/oven/io.hpp></pstade/oven/offset.hpp></iostream>…

pstade::oven::window(n, m)

#include <iostream> #include <pstade/oven/window.hpp> #include <pstade/oven/io.hpp> int main(){ namespace oven = pstade::oven; int array[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; std::cout << (array|oven::window(1, 7)) << std::endl; return 0; } [出力] {0,1,2,3,4,5} pstade::oven::window(n, m) は、 b</pstade/oven/io.hpp></pstade/oven/window.hpp></iostream>…

2つの range の全ての組み合わせが欲しい

[お題] 2つの range の全ての組み合わせが欲しい。 #include <iostream> #include <pstade/oven/matrix.hpp> #include <pstade/oven/concatenated.hpp> #include <pstade/oven/cycled.hpp> #include <pstade/oven/distance.hpp> #include <pstade/oven/zipped.hpp> #include <pstade/oven/any_range.hpp> #include <boost/tuple/tuple.hpp> #i…</boost/tuple/tuple.hpp></pstade/oven/any_range.hpp></pstade/oven/zipped.hpp></pstade/oven/distance.hpp></pstade/oven/cycled.hpp></pstade/oven/concatenated.hpp></pstade/oven/matrix.hpp></iostream>

pstade::oven::cycled をしたときに連続して展開して欲しい。

[お題] pstade::oven::cycled した時に、連続して展開して欲しい。 言葉にするとややこしいんですが、こんな感じです。 {0, 1, 2}|pstade::oven::cycled(4); // {0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2} // // ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ // // こんな感じで展開して…

コンストラクタとデストラクタの必要性って何よ

って、知り合いから質問が来たので、 「関数オブジェクトの引数が渡せないだろ情弱が!」 などと適当に返したのですが、自分なりのまとめをば。 例によって、完全にチラシの裏なのでどうでもいい人は適当にスルーしてください。 中の人もそんなに詳しい訳じ…

7並べ 〜その3〜 トランプのデータ

#include <iostream> #include <string> #include <pstade/oven/io.hpp> #include <pstade/oven/transformed.hpp> #include <pstade/oven/identities.hpp> #include <pstade/oven/copied.hpp> #include <pstade/oven/zipped.hpp> #include <pstade/oven/distance.hpp> #include <pstade/oven/counting.hpp> #include </pstade/oven/counting.hpp></pstade/oven/distance.hpp></pstade/oven/zipped.hpp></pstade/oven/copied.hpp></pstade/oven/identities.hpp></pstade/oven/transformed.hpp></pstade/oven/io.hpp></string></iostream>

pstade::oven::fuzipped に boost::array を渡すとコンパイルエラー

つまりこういうこと。 typedef boost::array<int, 5> array_t; array_t array = {0, 1, 2, 3, 4}; // こいつでコンパイルエラー //oven::make_fuzipped(fusion::make_vector(array, array)); // 変数に代入してから渡したら OK fusion::vector<array_t, array_t> foo(array, array); ov</array_t,></int,>…

pstade::oven::cycled を使用したときに range として展開して欲しい

つまりこんな感じに展開されて欲しい。 {0,1,2}|cycled(3); // {{0,1,2},{0,1,2},{0,1,2}} std::string("無駄")|cycled(3); // {"無駄","無駄","無駄"} pstade::oven::cycled は、引数の数だけ range を結合して展開するので、上記のままでは思うように展開…

指定した範囲のみを保持するクラス

こんな感じの事がしたい。 // 保持出来る範囲を指定して定義 hoge<int, 0, 20> value = 10; value = 0; value = 20; value = 21; // 範囲外なのでエラー boost に見当たらなかったのでガシガシ書いてみた。 どこかに存在しそうですが・・・。 #include <assert.h> #include <boost/integer.hpp> #include <boost/static_assert.hpp></boost/static_assert.hpp></boost/integer.hpp></assert.h></int,>…

7並べ 〜その2〜

関連:7並べ コンセプトとしては、boost や pstade の外部ライブラリを使用してどれだけコードをすっきりさせるか。 最初はオブジェクト指向で地道に考えていこうかとも思ったけどコンセプトと違うので今回は割愛。 まぁオブジェクト指向は他の機会でやれば…

pstade::oven::initial_values

#include <vector> #include <list> #include <pstade/oven/initial_values.hpp> #include <pstade/oven/io.hpp> #include <pstade/oven/cycled.hpp> #include <pstade/oven/identities.hpp> int main(){ namespace oven = pstade::oven; std::vector<int> array = oven::initial_values(0, 1, 2, 3, 4); std::cout << (arr…</int></pstade/oven/identities.hpp></pstade/oven/cycled.hpp></pstade/oven/io.hpp></pstade/oven/initial_values.hpp></list></vector>