小编idi*_*dak的帖子

如何使用boost :: mpl来编写策略?

我使用类似下面的内容为我的应用程序编写策略:

策略类如下所示:

struct Policy {
  static void init();
  static void cleanup();
  //...
};

template <class CarT, class CdrT>
struct Cons {
  static void init() {
    CarT::init();
    CdrT::init();
  }
  static void cleanup() {
    CdrT::cleanup();
    CarT::cleanup();
  }
  //...
};
Run Code Online (Sandbox Code Playgroud)

撰写政策:

typedef Cons<Policy1, Cons<Policy2, Cons<Policy3, Policy4> > > MyPolicy;
Run Code Online (Sandbox Code Playgroud)

要使用MyPolicy:

init_with<MyPolicy>(...);
//...
cleanup_with<MyPolicy>(...);
Run Code Online (Sandbox Code Playgroud)

他们打电话的地方:

MyPolicy::init_options(); // calls Policy1 to 4's init in order
Run Code Online (Sandbox Code Playgroud)

MyPolicy::cleanup(); // calls Policy1 to 4's cleanup in reverse order
Run Code Online (Sandbox Code Playgroud)

从本质上讲,Cons在这里构造一个类型列表.这很直接.然而,typedef cons line有点难看.拥有可以执行此操作的策略组合器是理想的:

typedef CombinePolicy<Policy1, Policy2, Policy3, Policy4> MyPolicy; …
Run Code Online (Sandbox Code Playgroud)

c++ templates metaprogramming boost-mpl

6
推荐指数
2
解决办法
2239
查看次数

哪种编译器适用于以下重载/特化行为?

请考虑以下代码:

#include <stdio.h>

namespace Foo {
  template <typename T>
  void foo(T *, int) { puts("T"); }

  template <typename T>
  struct foo_fun {
    static void fun() { foo((T *)0, 0); };
  };
}

namespace Foo {
  void foo(int *, int) { puts("int"); }
}

using namespace Foo;

int main() {
  foo_fun<int> fun;
  fun.fun();
}
Run Code Online (Sandbox Code Playgroud)

什么是预期产量?"T"还是int?

一个编译器(来自Apple的Xcode 3.1.2的gcc 4.0.1)输出"int",另外两个编译器(gcc 4.1.2和4.1.3)输出"T".

如果我在foo(T*,int)版本之前移动foo(int*,int)声明/定义,则全部输出"int".在这种情况下,当前标准是否定义了重载/特化的顺序?

c++ standards overloading specialization

4
推荐指数
1
解决办法
269
查看次数