コンパイル時にいくつかの構成を実行したいシミュレーションコードがあります。たとえば、ディメンション、データ型、および低レベルの操作(インライン化のためのコンパイル時)を含むクラスを定義する必要があります。
何かのようなもの:
template <int DIMENSION, class DATATYPE, class OPERATIONS>
class Simulation{ ... }
template <int DIMENSION, class DATATYPE, class OPERATIONS>
class SimulationNode{ ... }
template <int DIMENSION, class DATATYPE, class OPERATIONS>
class SimulationDataBuffer{ ... }
まず、各クラスのパラメータセット全体を記述するのは非常に面倒です。次に、さらに悪いことに、追加のパラメーターを導入する必要があり、すべてのクラスを変更する必要がある可能性があります。
テンプレートパラメータの構造体のようなものはありますか?
何かのようなもの
struct {
DIMENSION = 3;
DATATYPE = int;
OPERATIONS = SimpleOps;
} CONFIG;
template <class CONFIG>
class Simulation{ ... }
template <class CONFIG>
class SimulationNode{ ... }
template <class CONFIG>
class SimulationDataBuffer{ ... }
確かに、タイプのエイリアスとのstatic
メンバーを提供するクラステンプレートを作成しますint
。
template <int DIMENSION, class DATATYPE, class OPERATIONS>
struct Config
{
static constexpr int dimension = DIMENSION;
using datatype = DATATYPE;
using operations = OPERATIONS;
};
次に、次のように使用できます。
template <class CONFIG>
class Simulation{
void foo() { int a = CONFIG::dimension; }
typename CONFIG::operations my_operations;
}
using my_config = Config<3, int, SimpleOps>;
Simulation<my_config> my_simulation;
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加