2011-01-01から1年間の記事一覧
phx::for_each より楽に使えるものをと. https://gist.github.com/1540409 void revival_example() { using boost::phoenix::arg_names::_1; using boost::phoenix::arg_names::_2; using boost::phoenix::local_names::_x; int array[5] = { 1, 2, 3, 4, 5…
SFINAE に使えない http://d.hatena.ne.jp/gintenlabo/20110420/1303288950 メタプログラミングをする上で不便ですね. 必ずしも「引数の型全てが変換される型」を返すとは限らない struct A { A(int) {} template <class T> A(T) = delete; }; char c = '$'; std::co</class>…
これは C++11 Advent Calendar 2011 の参加記事 (15日目) です. この記事では「Iterator ではなく Range を使おう」という話をします.既に何度も言われている話ではありますが,私も一度書いてみたかったのでこの機会に書きます.でもそんなに新しいことは…
これは Boost Advent Calendar 2011 の参加記事 (11 日目)です. 今日はせっかくの機会なので,個人的に関心の高い Egg について,ちょっと書いてみたいと思います.Egg を知らない人も多いと思いますので,ここでは入門・解説を書くのではなく,どのような…
こういう記事も必要かもしれないと思いました.内容はタイトルそのままです. はじめに はじめに,と書きましたがこの項は飛ばしても問題ないです. まず "lvalue" とか "rvalue" とかいうものが何についての概念なのかということですが,これらは C++ の "…
C++11 の規格には,tuple-like という言葉が登場します.これは所謂コンセプトで,tuple-like コンセプトを満たす型 T は,std::tuple_size<T>,std::tuple_element<I, T>,get<I>(t) を持ちます.標準で定義されたものでは,std::pair<T1, T2>,std::tuple<Types...>,std::array<T, N> がこれを</t,></types...></t1,></i></i,></t>…
久しぶりに range アダプタでも. 考え方は oven::sorted と同じで,range からイテレータのリストを作り,そのリストをシャッフルします.結果として forward range にも対応できます. random_shuffled.hpp (要 P-Stade) #ifndef IORATE_RANDOM_SHUFFLED_H…
512 回以上であることが推奨されています.GCC 4.6.1 ではデフォルトで 512 回です. repeated.cpp // apply f n times to x template <class F, class T> constexpr T repeated(F f, unsigned n, T x) { return n == 0 ? x : repeated(f, n - 1, f(x)); } constexpr int inc(i</class>…
コンテナごとムーブ std::vector<T> v1; // ... std::vector<T> const v2(std::move(v1)); move_iterator を使ってムーブ std::vector<T> v; // ... std::list<T> const l( std::make_move_iterator(v.begin()), std::make_move_iterator(v.end())); move アルゴリズムを</t></t></t></t>…
震源地: Codnote.net やりましょう. fibbuzz.cpp #if !defined(BOOST_PP_IS_ITERATING) #include <boost/preprocessor/iteration/iterate.hpp> #include <boost/preprocessor/slot/slot.hpp> #define BOOST_PP_VALUE 1 #include BOOST_PP_ASSIGN_SLOT(1) #define BOOST_PP_VALUE 1 #include BOOST_PP_ASSIGN_SLOT(2) #define BOOST_P…</boost/preprocessor/slot/slot.hpp></boost/preprocessor/iteration/iterate.hpp>
BOOST_BINARY を使う やっぱり基本はマクロですね.ある車輪は使いましょう. #include <boost/utility/binary.hpp> #include <boost/static_assert.hpp> BOOST_STATIC_ASSERT((BOOST_BINARY(1001 1100 1011 111) == 0x4E5F)); テンプレートを使う C++ と言えばテンプレートメタプログラミング.あまり大きな数を</boost/static_assert.hpp></boost/utility/binary.hpp>…
std::array<std::array<T, M>, N> も std::vector<std::array<T, M> > も, &a[i][j] == &a[0][0] + i * M + j が成立するという意味での連続性が保証されるか分かったもんじゃないですねかわいい2011-09-12 10:24:33 via webstd::array, M> に要素の連続性の保証がないなら*1,生の多次元配列(</std::array<t,></std::array<t,>…
某 KUIS の院試でスリープソートが出題されたらしいので,以前話題になったときに書きかけてそのままにしていた C++0x での実装を晒しておきます. まず sleep.cpp がこちら. constexpr int f(int x, int y, int n) { return y == 0 ? 0 : f(x * 2, y - 1, …
Boost.Phoenix の if で戻り値を返す - C++でゲームプログラミング Phoenix の if_else は then 節と else 節の common_type を返しますが,それらの型が異なる場合は variant に収めて返すという戦略もあるでしょう.Phoenix ではそのような条件演算子もユ…
curryN<F> で,N-ary なメタ関数クラス F をカリー化します. #ifndef CURRY_HPP #define CURRY_HPP #include <boost/mpl/bind.hpp> #include <boost/mpl/placeholders.hpp> #include <boost/mpl/protect.hpp> #include <boost/preprocessor/arithmetic/dec.hpp> #include <boost/preprocessor/cat.hpp> #include </boost/preprocessor/cat.hpp></boost/preprocessor/arithmetic/dec.hpp></boost/mpl/protect.hpp></boost/mpl/placeholders.hpp></boost/mpl/bind.hpp></f>
次のコードは gcc 4.6.0 と clang 2.9 でコンパイルの通るものです. template <class T = void> struct A {}; template <template <class ...> class U> struct B { U<> a; }; B<A> b; B<A> のインスタンス化中は U = A となっているわけですが,U<> が通るということは,U には A のデフォルト引数が使</a></a></template></class>…
Haskell では定義をそのまま書き下すことで不動点コンビネータを定義できるそうですが, fix :: (a -> a) -> a fix f = f (fix f) 魔法言語 C++0x も負けてはいないはずです. template <class F> struct fix_result { F m_f; template <class ...Args> auto operator()(Args &&...ar</class></class>…
元ネタ constexpr で扱えて,実行時の効率もよいデータ構造+アルゴリズム - とくにあぶなくないRiSKのブログ arrayにpush_back, push_fonrt - bigsleepの日記 index_tuple の技法を使って,constexpr 関数の呼び出しを 2 回に抑える実装を考えました. #inc…
double は非型テンプレートパラメータに使えないので,そのコンパイル時計算は constexpr 関数に頼るのが一般的だと思いますが,テンプレートでもやってやれないことはありません. // 有名な階乗の計算を double で行います // Gamma function ではありませ…
result_of とは 呼び出し可能な型と引数の型から,呼び出した場合の戻り値の型を導出するメタ関数です.例を示しましょう. typedef int (*char_to_int)(char); int digit_to_int(char c) { return c - '0'; } // char_to_int 型に,char 型の引数を渡した時…
Undocumented ですが,pstade/egg/monad/ 以下に Monad クラスと boost::optional をそのインスタンスとしたものがあります.実用向きとは言えませんが,ちょっと遊んでみました. monad_optional_test.cpp #define BOOST_RESULT_OF_USE_DECLTYPE #include <cmath> </cmath>…
追記: 名前を変更 (elements → braced2).boost::array に限らず { { args... } } で初期化できるものなら大体使えます.また少し変更すれば { args... } などで初期化することもできます. C++03 では,配列型のメンバ変数を初期化子リストで初期化すること…
Data.Function on :: (b -> b -> c) -> (a -> b) -> a -> a -> c on f g = \x y -> f (g x) (g y) これの引数の数と型を一般化したものを,C++ を使っていくつかの方法で書いてみました.Variadic な関数オブジェクトを書く上で参考になるかもしれません. …
以下のようなことを可能にします。 string_test.cpp #include <iostream> #include "./string.hpp" template <class> struct print; template <char ...String> struct print<udon::string<String...>> { void operator()() const { char const s[] = { String..., 0 }; std::cout << s << std::endl; } }; int main() </udon::string<string...></char></class></iostream>…
Sandbox にありました。Fusion を C++0x に移植する試みのようです (参照)。 https://svn.boost.org/svn/boost/sandbox/SOC/2009/fusion/自分でも書いてみました。GCC 4.5, GCC 4.6, VC10 に対応しています。 ヘッダ https://gist.github.com/996989 テスト …
前回の続きです。今回は、Boost.Move による引数の転送を中心に解説していきたいと思います。 前回の補足 Boost.Move を使う利点 重要なことに触れていませんでした。 Move semantics は、乱暴に言えば「死すべきオブジェクト (rvalue) からは破壊的コピーを…
はじめに Boost.Move は、C++0x で導入される move sematics を C++03 でエミュレートするべく、Ion Gaztanaga 氏 (Boost.Interprocess の作者) によって開発されたライブラリです。既に Boost 入りが決定しており、現在 trunk にあります。 この記事は、そ…
※ Boost.Move の入っている revision について書きます Boost.Interprocess には Boost.Move に対応したコンテナが付属していますが、その中に std::array に相当するものはありません。これは、move constructor などを追加することにより、array が aggreg…
Variadic Templates を使うことによって、驚くほど簡潔に書くことができます (Egg に合わせて fuse という名前にしています)。 #include <cstddef> #include <tuple> #include <type_traits> #include <utility> namespace fuse_detail { template <std::size_t ...Indexes> struct index_tuple { typedef index_tuple<Indexes..., sizeof...(Indexes)> next;</indexes...,></std::size_t></utility></type_traits></tuple></cstddef>…
Haskell には関数適用演算子 $ があり、しばしば括弧を省く目的で用いられます。C++ でもやってみましょう。 #include <utility> namespace apply { template <class F, class A> inline auto operator|=(F &&f, A &&a) -> decltype(std::forward<F>(f)(std::forward<A>(a))) { return std::fo</a></f></class></utility>…