ここで、BOOST_TYPEOF を実装する一般的な考え方を理解したいと思います。コードは問題ないかもしれませんが、実際の Boost 実装の場合と同様に、コードは単純ではないと思います。したがって、BOOST_TYPEOF 実装の考え方を理解したいと思います。コンパイル時に式の型を理解するためにコンパイラ関数 (いくつかの API) を使用しますか?
2 に答える
コアでは、Boost::Typeof はsizeof
評価されていないコンテキストを使用して式の型を整数に変換し、それを型に戻します。
検討:
template<int N> struct sizer { char value[N]; };
sizer<1> encode(char);
sizer<2> encode(unsigned char);
sizer<3> encode(signed char);
sizer<4> encode(bool);
...
template<int N> struct decode {};
template<> struct decode<1> { typedef char type; };
template<> struct decode<2> { typedef unsigned char type; };
template<> struct decode<3> { typedef signed char type; };
template<> struct decode<4> { typedef bool type; };
#define TYPEOF(expr) decode<sizeof(encode(expr))>::type
ここで、任意のchar
型または に評価される式が与えられた場合、次のbool
ように記述できます。
TYPEOF(expr) var = expr;
Boost::Typeof は基本的に、1997 年に Brian Parker によって最初に発明されたこのアイデアの拡張です。アイデアの議論と歴史については、ポータブルな「typeof」演算子を参照してください。
テンプレートは、このスキームでは少し問題になります。std::pair
再帰の前であっても、型空間の 2 乗を与えるのと同じくらい単純なものです。Boost::Typeof は、テンプレートの型とそのパラメーターの型をコンパイル時のリンク リストの連続するスロットにエンコードすることで、これを解決します。
template<typename List> struct sizer {
char item0[List::at<0>];
char item1[List::at<1>];
char item2[List::at<2>];
...
};
template<typename List> struct encode_type<List, char>: append<List, 1> {};
template<typename List> struct encode_type<List, unsigned char>: append<List, 2> {};
template<typename List, typename S, typename T>
struct encode_type<List, std::pair<S, T> >:
encode_type<encode_type<append<List, 99>, S>, T> {};
template<typename Iter> struct decode_type<1, Iter> {
typedef char type;
typedef Iter iter;
};
template<typename Iter> struct decode_type<2, Iter> {
typedef unsigned char type;
typedef Iter iter;
};
template<typename Iter> struct decode_type<99, Iter> {
typedef typename decode_type<Iter::next::value, Iter::next>::type S;
typedef typename decode_type<Iter::next::value, Iter::next>::iter S_iter;
typedef typename decode_type<S_Iter::next::value, S_Iter::next>::type T;
typedef typename decode_type<S_Iter::next::value, S_Iter::next>::iter T_iter;
typedef std::pair<S, T> type;
typedef T_iter iter;
};
template<typename List, typename T>
sizer<typename encode_type<List, T>::type> encode(const T&);
template<typename List> struct decode {
typedef typename decode_type<List::begin::value, List::begin>::type type; };
#define TYPEOF(expr) decode<list<
sizeof(encode(expr).item0),
sizeof(encode(expr).item1),
sizeof(encode(expr).item2),
...
> >::type
これは、既存のコンパイル時のリンク リストの実装を前提としています。std::pair
デコーダーは、パラメーターの型に必要な数だけリスト反復子から項目を消費することに気付くでしょう。これは基本的に、可変型を持たない言語で同等の機能コードをそのまま翻訳したものです。
省略記号で示されている 2 行では...
、型の複雑さの固定レベル (つまり、推論したい型を構成するテンプレートと型の数) に制限されています。Boost::Typeof は、この制限に対してデフォルトで 50 を持っていますが、効率のためにそれを減らしたり、非常に複雑なプログラムのためにそれを増やしたりすることができます。
sizeof
これは、コンパイル時の演算子であり、テンプレート引数として使用できるという考えに基づいています。このようにして、各型に整数を割り当てることができ、その整数を使用して型に戻すことができます。欠点は、各タイプを手動で登録する必要があることです。
例えば:
#include <boost/preprocessor/stringize.hpp>
#include <cstddef>
#include <iostream>
template<size_t> struct TypeId;
#define REGISTER_TYPE(T, id) \
template<> struct TypeId<id> { \
char value[id]; \
typedef T type; \
static char const* const name; \
}; \
char const* const TypeId<id>::name = BOOST_PP_STRINGIZE(T); \
TypeId<id> type_to_id(T);
#define TYPEID_(value) TypeId<sizeof(type_to_id(value))>
#define TYPEOF(value) typename TYPEID_(value)::type
#define TYPENAME(value) TYPEID_(value)::name
REGISTER_TYPE(int, 1)
REGISTER_TYPE(unsigned int, 2)
// and so on for all built-in types
int main() {
int x;
TYPEOF(x) y;
std::cout << TYPENAME(y) << '\n';
}
出力:
./test
int