[英]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.