模板元编程 --- 用 MPL 解决实际问题
来源:互联网 发布:morris.js api中文 编辑:程序博客网 时间:2024/04/29 19:51
在 STL 中有各种容器,而 STL 算法允许我们对容器中的元素做各种操作,下面的程序对于每一个当代的 C++ 程序员都应该是轻而易举的:
#include
#include
运行结果:
world
hello
hello
world
简单的东西往往能说明深刻的道理,在这个程序里,我们遇到的本质问题是什么?首先,我们有一个容器;其次,我们可以往容器里面放东西,最后,我们可以通过算法把一个操作施加于这个容器中的每一个(也可以是部分)元素中。这就是上面程序中凝结的本质问题。
MPL 可以看成是 STL 的编译期版本,或者说元编程版本。它同样也提供了各种容器,只不过容纳的对象不是数据,而是类型。它们的构造方式语法上比较类似,或者甚至,我以为,更有趣一点:
#include
#include
#include
#include
#include
using namespace boost;
int main()
{
typedef mpl::list<> type_list1;
typedef mpl::push_front::type type_list2;
typedef mpl::push_front::type type_list;
// 或者这样更好
typedef mpl::list another_list;
std::cout << typeid(mpl::at_c::type).name() << std::endl;
std::cout << typeid(mpl::at_c::type).name() << std::endl;
std::cout << typeid(mpl::at_c::type).name() << std::endl;
std::cout << typeid(mpl::at_c::type).name() << std::endl;
}
稍微解释一下。mpl::list 就是 std::list 的元编程版本,而 mpl::push_front 是什么就不用我说了。mpl::at_c 是一个元编程算法,作用相当于运行期的 [ ] 运算符,也就是得到一个容器中在某个位置上的元素。在 VC7.1 下面,执行结果是
class std::basic_string,class std::allocator >
int
int
class std::basic_string,class std::allocator >
这跟运行期的 list 的行为几乎完全一致。
当然,mpl 也有 for_each ,而且我们也可以为 for_each 提供一个元编程 functor 。什么是元编程 functor ?运行时的 functor 是一个提供了 operator() 重载的 struct ,而元编程 functor 就是一个提供了 operator() 模板的 struct :
#include
#include
#include
#include
#include
#include
using namespace boost;
struct print
{
template
void operator()(const T&)
{
std::cout << typeid(T).name() << std::endl;
}
};
int main()
{
typedef mpl::list<> type_list1;
typedef mpl::push_front::type type_list2;
typedef mpl::push_front::type type_list;
typedef mpl::list another_list;
mpl::for_each(print());
mpl::for_each(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
#include
#include
#include
#include
#include
#include
#include
#include
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
void operator()(const T& product)
{
std::cout << "Type: " << typeid(T).name()
<< " SerialNo: " << product.SerialNo() << std::endl;
}
};
// 由于 PC 和 Print 都没有默认的 constructor ,必须加上这个
template
struct wrap {};
struct Create
{
Create(const std::string& _line)
: line_(_line)
, serial_(0)
{}
template
void operator()(wrap)
{
std::stringstream ss;
ss << line_ << '_' << serial_++;
shared_ptr product(new T(ss.str()));
print()(*product);
}
std::string line_;
unsigned long serial_;
};
int main()
{
typedef mpl::list product_list;
mpl::for_each >(Create("line1"));
}
输出:
Type: class Printer SerialNo: line1_0
Type: class PC SerialNo: line1_1
#include
#include
#include
#include
using namespace std;
struct print
{
void operator()(const string& _str)
{
cout << _str << endl;
}
};
int main()
{
list
str_list.push_front("hello");
str_list.push_front("world");
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
#include
#include
#include
#include
using namespace boost;
int main()
{
typedef mpl::list<> type_list1;
typedef mpl::push_front
typedef mpl::push_front
// 或者这样更好
typedef mpl::list
std::cout << typeid(mpl::at_c
std::cout << typeid(mpl::at_c
std::cout << typeid(mpl::at_c::type).name() << std::endl;
std::cout << typeid(mpl::at_c::type).name() << std::endl;
}
稍微解释一下。mpl::list 就是 std::list 的元编程版本,而 mpl::push_front 是什么就不用我说了。mpl::at_c 是一个元编程算法,作用相当于运行期的 [ ] 运算符,也就是得到一个容器中在某个位置上的元素。在 VC7.1 下面,执行结果是
class std::basic_string
int
int
class std::basic_string
这跟运行期的 list 的行为几乎完全一致。
当然,mpl 也有 for_each ,而且我们也可以为 for_each 提供一个元编程 functor 。什么是元编程 functor ?运行时的 functor 是一个提供了 operator() 重载的 struct ,而元编程 functor 就是一个提供了 operator() 模板的 struct :
#include
#include
#include
#include
#include
#include
using namespace boost;
struct print
{
template
void operator()(const T&)
{
std::cout << typeid(T).name() << std::endl;
}
};
int main()
{
typedef mpl::list<> type_list1;
typedef mpl::push_front
typedef mpl::push_front
typedef mpl::list
mpl::for_each
mpl::for_each(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
#include
#include
#include
#include
#include
#include
#include
#include
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
void operator()(const T& product)
{
std::cout << "Type: " << typeid(T).name()
<< " SerialNo: " << product.SerialNo() << std::endl;
}
};
// 由于 PC 和 Print 都没有默认的 constructor ,必须加上这个
template
struct wrap {};
struct Create
{
Create(const std::string& _line)
: line_(_line)
, serial_(0)
{}
template
void operator()(wrap
{
std::stringstream ss;
ss << line_ << '_' << serial_++;
shared_ptr
print()(*product);
}
std::string line_;
unsigned long serial_;
};
int main()
{
typedef mpl::list
mpl::for_each
}
输出:
Type: class Printer SerialNo: line1_0
Type: class PC SerialNo: line1_1
- 模板元编程 --- 用 MPL 解决实际问题
- 模板元编程 --- 用 MPL 解决实际问题
- 模板元编程 --- 用 MPL 解决实际问题
- 模板元编程 --- 用 MPL 解决实际问题
- 模板元编程 --- 用 MPL 解决实际问题
- 模板元编程 --- 用 MPL 解决实际问题
- 模板元编程 --- 用 MPL 解决实际问题
- 模板元编程 --- 用 MPL 解决实际问题
- 用自定义函数解决实际问题
- Java网络编程,有助于解决实际问题,
- 浅谈编程解决实际问题的常见思想
- 浅谈编程解决实际问题的常见思想
- 编程解决实际问题--预测比赛结果,确定凶手
- 模板--模板元编程
- C++元编程和Boost编程库 (C++ Metaprogramming and Boost MPL )上部
- C++元编程和Boost编程库 (C++ Metaprogramming and Boost MPL )中部
- C++元编程和Boost编程库 (C++ Metaprogramming and Boost MPL )
- C++元编程和Boost编程库 (C++ Metaprogramming and Boost MPL )上部
- Expression Template
- 获取和安装 FleaPHP
- 简单常识——关于string
- Template Metaprogramming - cont.1铪
- Template Metaprogramming
- 模板元编程 --- 用 MPL 解决实际问题
- FleaPHP 开发指南 - A2. 使用 PATHINFO 和 URL 重写
- 凤凰涅磐 --- Phoenix 2 发布预览
- 简单常识——关于stream
- unspecified_bool_type 手法
- FleaPHP 开发指南 - 8. 如何使用 RBAC 组件实现访问控制
- Java Double-Checked Locking 已死,C++ 呢?
- C++ Multithreading
- What you can do is more important than who your parents are.