作者godfat (godfat 真常)
看板C_and_CPP
标题Re: [语法] Template与Inheritance结合??
时间Tue Apr 18 14:26:54 2006
唔,还是讲一下好了
※ 引述《yoco315 (眠月)》之铭言:
: 很简单的继承,Foo 继承 Bar,没有指明继承的形式表示是 public 继承。
^^^^^^
是 private 推文说了,建议修文
: ┌───────────────────────────
: │template < class T >
: │class Foo : T
: │{
: │ ...
: │} ;
: └───────────────────────────
: 看起来很玄,其实就是 Foo 继承 T,
: 不过这个 T 是个 template,
我会说 T 是个 class 而非 template, 就像上篇我在推文说的
理由出自 template template (or more template) parameter
template < template <class> class T >
class Foo;
T 是什麽?不是 class, 是 template
Foo<int> foo; // error, int is not template
template <class T>
class Bar;
Foo<Bar> foo; // ok, Bar is template
Bar<Bar> bar; // error, Bar is not class
Bar< Bar<int> > // ok, Bar<int> is class
可以玩更复杂一点
template < template < template<class> class T > class U >
class Test{};
template < template<class> class T >
class Test2: private T<int>{}; // 写 T 不行,因为不能继承 template
Test<Test2> test; // ok, Test2 is template template
: Loki::Typelist 还有用到类似这样的写法
: ┌───────────────────────────
: │template < class T, class U >
: │Foo : T < U >
: │{
: │}
: └───────────────────────────
不对吧,Typelist 是这样做的
template <class Head, class Tail>
class Typelist{};
然後是这样串的
Typelist< int, Typelist<int, Typelist<int, Typelist<int, NullType> > > >
头咬尾巴这样
你说的东西後述
: 这可以用来实作 Typelist 技术。
^^^^^^^^
是 Tuple
: ┌───────────────────────────
: │template < typenamep T >
: │class Holder
: │{
: │ private: T value_ ;
: │ public: T & value() { return value_ ; }
: │} ;
: │
: │typedef TYPELIST_3 ( Array2D, Array2D, Array2D ) RGB_Typelist ;
: │typedef TYPELIST_3 ( Array2D, Array2D, Array2D ) HSV_Typelist ;
: │typedef TYPELIST_3 ( Array2D, Array2D, Array2D ) Lab_Typelist ;
: │typedef TYPELIST_4 ( Array2D, Array2D, Array2D, Array2D ) YMCK_Typelist ;
: │
: │typedef Loki::GenScatterHierarchy < RGB_Typelist , Holder > RGB_Image ;
: │typedef Loki::GenScatterHierarchy < HSV_Typelist , Holder > HSV_Image ;
: │typedef Loki::GenScatterHierarchy < Lab_Typelist , Holder > Lab_Image ;
: │typedef Loki::GenScatterHierarchy < YMCK_Typelist, Holder > YMCK_Image ;
: └───────────────────────────
参考 Loki::Tuple, 你说的这个跟 Loki::Tuple 做法很接近(其实算一样了)
而这里的 Typelist 是用头咬尾做的,GenScatterHierarchy 才是运用继承技术
不过真正的实作法很复杂,这里就不说明了…(其实是我还没完全看懂 Orz)
: RGB_Image img ;
: Loki::Field<0>(img).value() = ...
: Loki::Field<1>(img).value() = ...
: Loki::Field<2>(img).value() = ...
Loki 的 Tuple 的 holder 是这样写的
template <class T>
struct TupleUnit
{
T value_;
operator T&() { return value_; }
operator const T&() const { return value_; }
};
所以用 Loki::Tuple 的话是这样写
RGB_Image img;
Field<0>(img) = 255;
Field<1>(img) = 255;
Field<2>(img) = 255;
Loki:: 可以省略,因为 Koenig Lookup...
: 真是酷!
: Loki::Typelist 会根据你给的 Typelist,
: (就是一串型别,像是这样 int, int, double, Foo, Bar )
: 使用递回继承自己的方法,
这是 GenScatterHierarchy...
GenScatterHierarchy 会依照 Typelist 里面的型别依序继承
Typelist 就只是型别串列而已
: 实作细节有兴趣的话可以看 《Modern C++ Design》。
indeed...
不过看原始码更快
http://sourceforge.net/projects/loki-lib
现在 0.14 了,里面多一堆书上没写的东西
Sutter 的 Pimpl 也有(0.14 新增)
ScopeGuard(0.13 新增)
还有一堆有兴趣自己看吧…
--
Nobody can take anything away from him.
Nor can anyone give anything to him.
What came from the sea,
has returned to the sea.
Chrono Cross
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.138.19.204
※ 编辑: godfat 来自: 140.138.19.204 (04/18 14:32)