简体   繁体   English

在编译时选择实现

[英]Choose between implementations at compile time

Say one wants to create a C++ class with two separate implementations (say one to run on a CPU and on a GPU) and one wants this to happen at compile time. 假设有人想要创建一个带有两个独立实现的C ++类(比如一个在CPU和GPU上运行),并且希望在编译时发生这种情况。

What design pattern can be used for this? 可以使用什么样的设计模式?

A good book to read is: Modern C++ Design: Generic Programming and Design Patterns Applied, written by Andrei Alexandrescu. 一本好书要读:现代C ++设计:应用的通用编程和设计模式,由Andrei Alexandrescu编写。

Basicly he said that you can implement what you want using policy based class (a kind of strategy pattern, but done at compilation time. Bellow is a simple example showing this: 基本上他说你可以使用基于策略的类来实现你想要的东西(一种策略模式,但是在编译时完成.Bellow是一个简单的例子,显示了这个:

#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();
}

You can use a simple template for that. 你可以使用一个简单的模板。 (Sorry for the crude implementation, this is just an example) (对不起粗略的实现,这只是一个例子)

#include <iostream>
struct example
{
    void cpu() { std::cout << "Cpu\n"; }
    void gpu() { std::cout << "Gpu\n"; }

    template<bool useGpu = true>void go() { gpu(); }
};
template<>void example::go<false>() { cpu(); }

int main()
{
    example().go<false>(); //<-- Prints 'Cpu'
    example().go(); // <-- Prints 'Gpu'
}

Simplest solution example, using the Strategy pattern (however, it's irrelevant whether it's compile-time or run-time chosen): 最简单的解决方案示例,使用策略模式(但是,无论是编译时还是选择运行时都无关紧要):

class BaseStrategy
{
  public:
    virtual void doStuff() = 0;  
};

class Strategy1 : public Base
{
  public:
    void doStuff();
};

class Strategy2 : public Base
{
  public:
    void doStuff();
};

class SomeKindOfAMainClass
{
  public:
    SomeKindOfAMainClass(BaseStrategy* s)
    {
      this->s = s; 
    }
    void doStuff()
    {
      s->doStuff();
    }
  private:
    BaseStrategy* s;
};

and then you just do either new SomeKindOfAMainClass(new Strategy1()) or new SomeKindOfAMainClass(new Strategy2()) . 然后你只需要new SomeKindOfAMainClass(new Strategy1())new SomeKindOfAMainClass(new Strategy2())

Simple example of traits: 特征的简单例子:

struct WithStrategy1 {};
struct WithStrategy2 {};

template<typename T>
class SomeKindOfAMainClass;

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

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

And you just either instantiate SomeKindOfAMainClass<WithStrategy1> or SomeKindOfAMainClass<WithStrategy2> at the beginning of your program. 您只需在程序开头实例化SomeKindOfAMainClass<WithStrategy1>SomeKindOfAMainClass<WithStrategy2>

Or you can have the solution given by Omaha with #ifdef . 或者你可以通过#ifdef获得奥马哈给出的解决方案。

If you want to make compile-time decisions, there is always the old standby: the preprocessor. 如果你想做出编译时的决定,总会有旧的备用数据库:预处理器。 Use #ifdef / #endif blocks and compiler arguments to specify the code you want. 使用#ifdef / #endif块和编译器参数指定所需的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C++:有两种实现之间选择的好方法吗? - C++ : Is there a good way to choose between 2 implementations? 有没有一种方法可以根据模板参数的类型在不同的类实现之间进行选择? - Is there a way to choose between different class implementations based on the type of the template argument? 在编译时选择随机数分布 - Choose random number distribution at compile time 使用较旧的C ++实现在编译时检查一些编译时定义 - Check some compile-time definitions at compile time with older C++ implementations 如何基于constexpr结果在编译时选择函数 - How can I choose a function at compile time based on a constexpr result 在编译时选择C ++字符串文字的默认类型和编码 - Choose default type and encoding for C++ string literals at compile time 编译时条件包含。 分割标题与包括整个实现 - Compile time conditional inclusion. Segmenting headers vs including whole implementations 面向对象:如何从多个实现中进行选择 - Object Orientation: How to Choose from a Number of Implementations 在编译时在float和double精度之间切换 - Switching between float and double precision at compile time 有没有办法在编译时和运行时之间专门化一个函数? - Is there a way to specialize a function between compile time and run time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM