Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在编译时选择实现 希望使用两个单独的实现(例如,在CPU上运行,在GPU上运行)来创建一个C++类,并且希望在编译时发生这种情况。p>_C++_Design Patterns - Fatal编程技术网

在编译时选择实现 希望使用两个单独的实现(例如,在CPU上运行,在GPU上运行)来创建一个C++类,并且希望在编译时发生这种情况。p>

在编译时选择实现 希望使用两个单独的实现(例如,在CPU上运行,在GPU上运行)来创建一个C++类,并且希望在编译时发生这种情况。p>,c++,design-patterns,C++,Design Patterns,什么样的设计模式可以用于此?如果您想在编译时做出决策,总是有一个老的备用:预处理器。使用#ifdef/#endif块和编译器参数来指定所需的代码。您可以使用一个简单的模板。 (很抱歉,实施粗糙,这只是一个示例) #包括 结构示例 { void cpu(){std::cout最简单的解决方案示例,使用该模式(但是,选择编译时还是运行时无关): 然后您只需执行newsomekindofamain类(newstrategy1())或newsomekindofamain类(newstrategy2())

什么样的设计模式可以用于此?

如果您想在编译时做出决策,总是有一个老的备用:预处理器。使用
#ifdef
/
#endif
块和编译器参数来指定所需的代码。

您可以使用一个简单的模板。 (很抱歉,实施粗糙,这只是一个示例)

#包括
结构示例
{

void cpu(){std::cout最简单的解决方案示例,使用该模式(但是,选择编译时还是运行时无关):

然后您只需执行
newsomekindofamain类(newstrategy1())
newsomekindofamain类(newstrategy2())

特征的简单示例:

struct WithStrategy1 {};
struct WithStrategy2 {};

template<typename T>
class SomeKindOfAMainClass;

template<>
class SomeKindOfAMainClass<WithStrategy1>
{
  //use Strategy1 here
};

template<>
class SomeKindOfAMainClass<WithStrategy2>
{
  //use Strategy2 here
};
struct with strategy1{};
Strategy2{}的结构;
模板
类是一种主要类;
模板
类的某个类
{
//在这里使用策略1
};
模板
类的某个类
{
//在这里使用策略2
};
您只需在程序开始时实例化
SomeKindOfAMainClass
SomeKindOfAMainClass


< Omaha >的代码可以是:<代码> > IFIFF < /C> > < /P> < P>一本好的阅读书籍是:现代C++设计:泛型编程和应用模式,由Andrei Alexandrescu编写。 基本上,他说您可以使用基于策略的类(一种策略模式,但在编译时完成)实现您想要的内容。下面是一个简单的示例,说明了这一点:

#include <iostream>

using namespace std;

template <typename T>
struct CPU
{
  // Actions that CPU must do (low level)
  static T doStuff() {cout << "CPU" << endl;};
};

template <typename T>
struct GPU
{
  // Actions that GPU must do (low level)
  // Keeping the same signatures with struct CPU will enable the strategy design patterns
  static T doStuff() {cout << "GPU" << endl;};
};

template <typename T, template <class> class LowLevel>
struct Processors : public LowLevel<T>
{
  // Functions that any processor must do
  void process() {
    // do anything and call specific low level
    LowLevel<T>::doStuff();
  };
};

int main()
{
  Processors<int, CPU> cpu;
  Processors<int, GPU> gpu;

  gpu.process();
  cpu.process();
}
#包括
使用名称空间std;
模板
结构CPU
{
//CPU必须执行的操作(低级别)

静态T doStuff(){如果您想在编译时决定,就不需要设计模式。只需实例化您想要的实现。如果您想通过在源文件中切换一些“设置”来实现这一点,那么您可以使用一个简单的模板(或者用于
C
样式的ifdef)。模板不是在运行时决定的吗?可以给出一个例子(我接受这个答案)C++模板是编译时实体。如果你有很多函数,你必须为每个函数创建三个函数而不是IF方法。这两个方法在2个方法之间选择了SimPy的例子。对于你想要的,你可能想在其中使用一个多个方法的类。BartoszKP的答案是更多。然后面向您正在寻找的内容…@nbubis:在本例中,您不必编写完整实现的cpu()和gpu()。核心功能将位于单独的函数中:它将直接从cpu()调用,并通过从gpu()提交给gpu的内核函数调用。基本上,只有GPU提交所需的簿记需要单独编写(它们总是这样);其余的可以是常见的。对我来说,这其中复杂的部分是,我希望编译工具链注入策略选择。我最终回到宏,失去了策略抽象。
struct WithStrategy1 {};
struct WithStrategy2 {};

template<typename T>
class SomeKindOfAMainClass;

template<>
class SomeKindOfAMainClass<WithStrategy1>
{
  //use Strategy1 here
};

template<>
class SomeKindOfAMainClass<WithStrategy2>
{
  //use Strategy2 here
};
#include <iostream>

using namespace std;

template <typename T>
struct CPU
{
  // Actions that CPU must do (low level)
  static T doStuff() {cout << "CPU" << endl;};
};

template <typename T>
struct GPU
{
  // Actions that GPU must do (low level)
  // Keeping the same signatures with struct CPU will enable the strategy design patterns
  static T doStuff() {cout << "GPU" << endl;};
};

template <typename T, template <class> class LowLevel>
struct Processors : public LowLevel<T>
{
  // Functions that any processor must do
  void process() {
    // do anything and call specific low level
    LowLevel<T>::doStuff();
  };
};

int main()
{
  Processors<int, CPU> cpu;
  Processors<int, GPU> gpu;

  gpu.process();
  cpu.process();
}