Typelistについては扱っていないのか?と思いましたが、それなりにページが割かれて
いるようです。
書く人が違うと実装も様々ですね。 LokiのTypelistでは TypeAt操作は下記のような感じ
です。
template <class TList, unsigned int index>
struct TypeAt;
template <class Head, class Tail>
struct TypeAt<Typelist<Head, Tail>, 0>
{
typedef Head Result;
};
template <class Head, class Tail, unsigned int i>
struct TypeAt<Typelist<Head, Tail>, i>
{
typedef typename TypeAt<Tail, i - 1>::Result Result;
};
これと同様なものが Advanced C++ Metaprogrammingでは次のような実装が紹介されて
います。
template <class TList, unsigned int index>
struct TypeAt;
template <class T, class U, unsigned int index>
struct TypeAt<Typelist<T, U>, index> : public TypeAt<U, index - 1>
{
};
template <class T, class U>
struct TypeAt<Typelist<T, U>, 0>
{
typedef T Result;
};
template metaprogrammingではこのように継承を使うと場合によっては簡潔になる
ケースがあるんですね(ほとんど変わっていないという見方もありますが)。しかしながら、
採用しているコンパイラオプションによっては warningが出てしまうので、そういう点も
加味して実装を決定しましょう。
I'm now reading the book Advanced C++ Metaprogramming. When I first took a look
at the Table Of Contents, I didn't find that there is a section or chapter which talks
about Typelist. But there is.
I now realized that Typelist implementation should be varied. According to the
Loki implementation, TypeAt manipulation is defined something like below.
template <class TList, unsigned int index>
struct TypeAt;
template <class Head, class Tail>
struct TypeAt<Typelist<Head, Tail>, 0>
{
typedef Head Result;
};
template <class Head, class Tail, unsigned int i>
struct TypeAt<Typelist<Head, Tail>, i>
{
typedef typename TypeAt<Tail, i - 1>::Result Result;
};
On the other hand, the same TypeAt manipulation in Advanced C++ Metaprogramming
is defined something like this.
template <class TList, unsigned int index>
struct TypeAt;
template <class T, class U, unsigned int index>
struct TypeAt<Typelist<T, U>, index> : public TypeAt<U, index - 1>
{
};
template <class T, class U>
struct TypeAt<Typelist<T, U>, 0>
{
typedef T Result;
};
where we can see that using derivation or inheritance may contribute to being
simpler implementation in some sense. However, we may need to take more care
in case that we are going to use derivation / inheritance, since a certain compiler
option may give some warning.. (e.g. in case that we set up -Weffc++ option etc. in
GCC[g++]).
0 件のコメント:
コメントを投稿