「More C++ Idioms/メタ関数(Metafunction)」の版間の差分

削除された内容 追加された内容
M en:More C++ Idioms/Metafunction の 3版: oldid=1356705
Yak! (トーク | 投稿記録)
現内容を翻訳
1 行
=<center>メタ関数(Metafunction)</center>=
=== Intent意図 ===
* 複雑な型計算アルゴリズムをカプセル化する。
* To encapsulate a complex type computation algorithm
* コンパイル時型選択テクニックを用いて型を生成する。
* To generate a type using compile-time type selection techniques
 
=== Also Known As別名 ===
 
=== Motivation動機 ===
テンプレートは C++ の強力な機能であり、コンパイル時に任意の計算を実行することができる。これはテンプレートメタプログラミングとして知られている。コンパイル時に実行される計算の単純な例として (1) コンパイル時定数に基づく型の選択 (2) 階乗の計算 がある。実際のところ、C++ テンプレートは[http://ubiety.uwaterloo.ca/~tveldhui/papers/2003/turing.pdf チューリング完全]な C++ のサブ言語である。メタ関数(Metafunction)イディオムは C++ でコンパイル時アルゴリズムを記述する基本的な方法である。
Templates is a powerful feature of C++, which can be used to perform arbitrary computations at compile-time, which is known as template metaprogramming. Some of the basic examples of the computations performed at compile-time are: (1) selection of a type based on compile-time constants or (2) computing factorial of a number. As a matter of fact, C++ templates is a [http://ubiety.uwaterloo.ca/~tveldhui/papers/2003/turing.pdf turing complete] sub-language of C++. Metafunction idiom is the principle way of writing compile-time algorithms in C++.
 
利用あるいは再利用しやすいようにアルゴリズムは(コンパイル時に対するものでも実行時に対するものでも)カプセル化するべきである。一般的に、実行時アルゴリズムは、(当然)実行時に呼び出される関数としてカプセル化される。一方メタ関数とは、実行時での関数に対するコンパイル時の類似物である。通常の関数は値やオブジェクトをパラメータとして受け入れ、値やオブジェクトを返す。しかし、メタ関数は型やコンパイル時定数をパラメータとして受け入れ型や定数を返す。
Algorithms -- compile-time or run-time -- should be encapsulated so that they are easier to use and reuse. Conventionally, run-time algorithms are encapsulated in functions that are invoked, obviously, at run-time. Metafunctions, on the other hand, are compile-time analogs of run-time functions. Traditional functions accept values/objects as parameters and return values/objects. However, metafunctions accept types and compile-time constants as parameters and return types/constants.
 
=== Solution and Sample Code解法とサンプルコード ===
メタ関数はその名に反してクラステンプレートである。メタ関数の実装は大抵テンプレートの特殊化に基づいている。例えば、以下の ''IF'' メタ関数、すなわち実行時の ''if'' 文に対するコンパイル時の同等物を考えてみよう。以下の例では、最初のパラメータの値に依存して、''IF'' メタ関数は ''int'' か ''long'' を返す。
A metafunction, contrary to its name, is a class template. Implementation of metafunctions is often based on template specializations. For example, consider the following ''IF'' metafunction, which is compile-time equivalent of run-time ''if'' statement. Depending upon the value of the first parameter, ''IF'' metafunction yields either an ''int'' or a ''long'' in the example below.
 
<source lang="cpp">
27 行
};
 
IF<false, int, long>::type i; // is equivalent to long i; と等しい。
IF<true, int, long>::type i; // is equivalent to int i; と等しい。
</source>
 
以下の ''Factorial'' メタ関数は別の例であり、どのように再帰的な階乗計算アルゴリズムが C++ テンプレートを用いてカプセル化されるかを示す。このメタ関数は型ではなく整数値を返す。
''Factorial'' metafunction below is another example showing how a recursive factorial computation algorithm can be encapsulated using C++ templates. This metafunction yields an integral value rather than a type.
 
<source lang="cpp">
55 行
</source>
 
'''メタ関数(Metafunction and )と型生成器(Type Generator)'''
 
メタ関数(Metafunction)は[[More C++ Idioms/型生成器(Type Generator)|型生成器(Type Generator)]]イディオムよりも汎用的なイディオムである。メタ関数(Metafunction)イディオムの意図はコンパイル時計算をカプセル化しようというものである一方、型生成器(Type Generator)は型の指定を単純化しようというものである。コンパイル時計算の結果として型を生成するメタ関数は、より複雑ではあるが、全て型生成器である。しかし、全てのメタ関数が型生成器であるわけではない。例えば、前記 ''Factorial'' メタ関数は計算の結果として整数値を生成し型を生成しない。型生成器と異なり、一般的にメタ関数は [[More C++ Idioms/コンパイル時制御構造(Compile Time Control Structures)|コンパイル時制御構造(compile-time control structures)]]か他のメタ関数を用いて実装される。
Metafunction is a more general idiom than the [[More C++ Idioms/Type Generator|type generator]] idiom. The intent of metafunction idiom is to encapsulate compile-time computation where as, type generator simplifies specification of a type. Metafunctions that produce type(s) as a result of a compile-time computation are all type generators, albeit more complex. However, not every metafunction is a type generator. For example, the ''Factorial'' metafunction shown before produces an integral value, not a type, at the end of the computation. Generally, metafunctions are implemented using [[More C++ Idioms/Compile Time Control Structures|compile-time control structures]] or other metafunctions unlike type generators.
 
[http://www.boost.org/doc/libs/release/libs/mpl Boost.MPL] のようなライブラリが C++ テンプレートメタプログラミングを単純化するために、メタ関数とコンパイル時データ構造の大規模なコレクションを提供している。
Libraries such as [http://www.boost.org/doc/libs/release/libs/mpl Boost.MPL] provide a large collection of metafunctions and compile-time data structures to simplify C++ template metaprogramming.
 
'''高階メタ関数'''
'''Higher order metafunctions'''
 
メタ関数をパラメータとして受け取って計算中に使用するメタ関数が存在する。概念的には、別の関数へのポインタあるいは関数オブジェクトをパラメータとして受け取る実行時関数と同様である。差はメタ関数がコンパイル時にのみ存在する点だけである。''boost::mpl::transform'' がそのような高階メタ関数の例である。
These are metafunctions that accept other metafunctions as parameters and use them during computation. This is conceptually is similar a function accepting a pointer to another function or a function object as a parameter at run-time. Only difference is that metafunctions exist only at compile-time. ''boost::mpl::transform'' is an example of such a higher order metafunction.
 
=== Known Uses既知の利用 ===
[http://www.boost.org/doc/libs/release/libs/mpl Boost.MPL]
 
=== Related Idioms関連するイディオム ===
* [[More C++ Idioms/型生成器(Type Generator)|型生成器(Type Generator)]]
 
=== References ===
[http://www.artima.com/cppsource/metafunctions.html A Deeper Look at Metafunctions] -- David Abrahams and Aleksey Gurtovoy
 
[[Category:{{BASEPAGENAME}}|{{SUBPAGENAME}}]]
<noinclude>
[[en:More C++ Idioms/Metafunction]]
</noinclude>
[[Category:{{BASEPAGENAME}}|{{SUBPAGENAME}}めたかんすう]]