■
最近やっとSerializeの実装が分かってきたような・・
で、そのSerializeの実装にmplとtype_traitsがよく出てくるんで
そっちのほうの実装も結構見てたりするんだけど
その中のis_polymorphicってのが、ずっとSerialize専用のものだと思ってて
気にもしてなかったんです。
それが、ふと開いたtype_traits.hppに
is_polymorphic.hppって。。あれれ?みたいな。
でも、is_polymorphicの実装ってどうやるんだろうなあって
実は気になってたもんだから、じゃあ見てみようと。
template
struct is_polymorphic_selector
{
template
struct rebind
{
typedef is_polymorphic_imp2type;
};
};template <>
struct is_polymorphic_selector
{
template
struct rebind
{
typedef is_polymorphic_imp1type;
};
};template
struct is_polymorphic_imp
{
typedef is_polymorphic_selector< ::boost::is_class::value> selector;
typedef typename selector::template rebindbinder;
typedef typename binder::type imp_type;
BOOST_STATIC_CONSTANT(bool, value = imp_type::value);
};
ふーん。メタ関数クラスかあ・・
別にたいした事やってないな・・
template
struct is_polymorphic_imp1
{
# if BOOST_WORKAROUND(__MWERKS__, <= 0x2407) // CWPro7 should return false always.
typedef char d1, (&d2)[2];
# else
typedef typename remove_cv::type ncvT;
struct d1 : public ncvT
{
d1();
# if !defined(__GNUC__) // this raises warnings with some classes, and buys nothing with GCC
~d1()throw();
# endif
char padding[256];
};
struct d2 : public ncvT
{
d2();
virtual ~d2()throw();
# if !defined(BOOST_MSVC) && !defined(__ICL)
// for some reason this messes up VC++ when T has virtual bases,
// probably likewise for compilers that use the same ABI:
struct unique{};
virtual void foo(unique*);
# endif
char padding[256];
};
# endif
BOOST_STATIC_CONSTANT(bool, value = (sizeof(d2) == sizeof(d1)));
};
いつでもそうだけど、どうやるのだろうって思うような
テクニックなりイディオムなりは、理解してしまうと
当たり前ジャンって思うけど、その当たり前さえ思いつけなかったんだよなーって。
テンプレート引数で受け取ったTをローカルクラスで継承しちゃう
あーはじめてみた。。こんなの。
メタ関数も初めて見たときはそんな風に思ったわけで
パターン化しちゃうことで考えることを捨てちゃいけないなぁ・・と
反省なのでした。
なんか今日の日記はまとまりないなあ・・