您的位置:首页 > 编程语言

模板元编程 --- 用 MPL 解决实际问题

2012-01-09 00:36 148 查看
模板元编程 --- 用 MPL 解决实际问题

转:http://blog.csdn.net/ralph623/archive/2005/10/15/504369.aspx

在 STL 中有各种容器,而 STL 算法允许我们对容器中的元素做各种操作,下面的程序对于每一个当代的 C++ 程序员都应该是轻而易举的:

#include <iostream>

#include <list>

#include <algorithm>

#include <string>

using namespace std;

struct print

{

void operator()(const string& _str)

{

cout << _str << endl;

}

};

int main()

{

list<string> str_list;

str_list.push_front("hello");

str_list.push_front("world");



list<string> another_list;

another_list.push_back("hello");

another_list.push_back("world");



for_each(str_list.begin(), str_list.end(), print());

for_each(another_list.begin(), another_list.end(), print());

}

运行结果:

world

hello

hello

world

简单的东西往往能说明深刻的道理,在这个程序里,我们遇到的本质问题是什么?首先,我们有一个容器;其次,我们可以往容器里面放东西,最后,我们可以通过算法把一个操作施加于这个容器中的每一个(也可以是部分)元素中。这就是上面程序中凝结的本质问题。

MPL 可以看成是 STL 的编译期版本,或者说元编程版本。它同样也提供了各种容器,只不过容纳的对象不是数据,而是类型。它们的构造方式语法上比较类似,或者甚至,我以为,更有趣一点:

#include <string>

#include <iostream>

#include <boost/mpl/at.hpp>

#include <boost/mpl/list.hpp>

#include <boost/mpl/push_front.hpp>

using namespace boost;

int main()

{

typedef mpl::list<> type_list1;

typedef mpl::push_front<type_list1, int>::type type_list2;

typedef mpl::push_front<type_list2, std::string>::type type_list;



// 或者这样更好

typedef mpl::list<int, std::string> another_list;



std::cout << typeid(mpl::at_c<type_list, 0>::type).name() << std::endl;

std::cout << typeid(mpl::at_c<type_list, 1>::type).name() << std::endl;



std::cout << typeid(mpl::at_c<another_list, 0>::type).name() << std::endl;

std::cout << typeid(mpl::at_c<another_list, 1>::type).name() << std::endl;

}

稍微解释一下。mpl::list 就是 std::list 的元编程版本,而 mpl::push_front 是什么就不用我说了。mpl::at_c 是一个元编程算法,作用相当于运行期的 [ ] 运算符,也就是得到一个容器中在某个位置上的元素。在 VC7.1 下面,执行结果是

class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >

int

int

class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >

这跟运行期的 list 的行为几乎完全一致。

当然,mpl 也有 for_each ,而且我们也可以为 for_each 提供一个元编程 functor 。什么是元编程 functor ?运行时的 functor 是一个提供了 operator() 重载的 struct ,而元编程 functor 就是一个提供了 operator() 模板的 struct :

#include <string>

#include <iostream>

#include <boost/mpl/at.hpp>

#include <boost/mpl/list.hpp>

#include <boost/mpl/push_front.hpp>

#include <boost/mpl/for_each.hpp>

using namespace boost;

struct print

{

template <class T>

void operator()(const T&)

{

std::cout << typeid(T).name() << std::endl;

}

};

int main()

{

typedef mpl::list<> type_list1;

typedef mpl::push_front<type_list1, int>::type type_list2;

typedef mpl::push_front<type_list2, std::string>::type type_list;



typedef mpl::list<int, std::string> another_list;



mpl::for_each<type_list>(print());

mpl::for_each<another_list>(print());

}

输出与上面使用 mpl::at_c 的程序完全相同。

当然,到现在为止,这些程序都还是只停留在纯粹的玩具程序上,能不能做点稍微有用的事情呢?当然可以。假定我们有这样一个继承体系:根是一个抽象类 Product ,它有一些派生类,例如 PC , Printer 等等,它们的公共方法 SerialNo 会返回自己的产品序列号,而这个序列号是在构造的时候决定的:

class Product

{

public:

virtual std::string SerialNo()const = 0;

};

class PC : public Product

{

public:

PC(const std::string& _sn)

: sn_(_sn)

{}

std::string SerialNo()const

{

return sn_;

}

private:

std::string sn_;

};

class Printer : public Product

{

public:

Printer(const std::string& _sn)

: sn_(_sn)

{}



std::string SerialNo()const

{

return sn_;

}

private:

std::string sn_;

};

用 mpl::list 把这些类型放在同一个 list 里面当然不在话下,但是我们希望有一个类似 factory 模式的实现,让我们可以自由创建它们。下面的程序用 mpl::for_each 为 list 中的每一个类型创建一个实例,它当然可以被扩展来做些很有用的事情。

#include <string>

#include <sstream>

#include <iostream>

#include <algorithm>

#include <list>

#include <boost/shared_ptr.hpp>

#include <boost/mpl/at.hpp>

#include <boost/mpl/list.hpp>

#include <boost/mpl/for_each.hpp>

using namespace boost;

class Product

{

public:

virtual std::string SerialNo()const = 0;

};

class PC : public Product

{

public:

PC(const std::string& _sn)

: sn_(_sn)

{}

std::string SerialNo()const

{

return sn_;

}

private:

std::string sn_;

};

class Printer : public Product

{

public:

Printer(const std::string& _sn)

: sn_(_sn)

{}



std::string SerialNo()const

{

return sn_;

}

private:

std::string sn_;

};

struct print

{

template <class T>

void operator()(const T& product)

{

std::cout << "Type: " << typeid(T).name()

<< " SerialNo: " << product.SerialNo() << std::endl;

}

};

// 由于 PC 和 Print 都没有默认的 constructor ,必须加上这个

template <class T>

struct wrap {};

struct Create

{

Create(const std::string& _line)

: line_(_line)

, serial_(0)

{}



template <class T>

void operator()(wrap<T>)

{

std::stringstream ss;

ss << line_ << '_' << serial_++;

shared_ptr<T> product(new T(ss.str()));



print()(*product);

}



std::string line_;

unsigned long serial_;

};

int main()

{

typedef mpl::list<Printer, PC> product_list;



mpl::for_each<product_list, wrap<mpl::_1> >(Create("line1"));

}

输出:

Type: class Printer SerialNo: line1_0

Type: class PC SerialNo: line1_1

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ralph623/archive/2005/10/15/504369.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: