我可以使用 C++ 模板来实现两个 类 ,它们只有一种字段类型不同吗?

Can I use C++ templates to implement two classes which only differ by one field type?

我正在尝试为 两个 系统实施 Controller class。每个 Controller 使用一个 Solver class 实例。

我已经有 两个 Solver class 实现:

class AbstractSolver { virtual void func() = 0; };
class System1Solver : AbstractSolver { void func() override; };
class System2Solver : AbstractSolver { void func() override; };

Controller 实现与 Solver 类型的 相同 。我可以实现两者Controllerclass就像...

class System1Controller { 
  public:
    System1Controller(System1Solver solver);
    System1Solver solver;
    void func() { solver.func(); };
};

class System2Controller { 
  public:
    System2Controller(System2Solver solver);
    System2Solver solver;
    void func() { solver.func(); };
};

我想复制和粘贴相同的代码。两个 System?Controller 实现 相同 ,除了 solver 类型 。我可以改用 templates 以某种方式实现代码重用吗?

template <class SolverClass>
class Controller {
  public:
    Controller(SolverClass solver);
    SolverClass solver;
    void func() { solver.func(); };
};

class System1Controller : Controller<System1Solver>;
class System2Controller : Controller<System2Solver>;

编辑: 这个例子确实有效。感谢大家确认这是完成此任务的合适方法。

/* Compile with g++ -O3 -std=c++14 -o crimes metaprogramming_crimes.cpp */

#include <iostream>

class AbstractSolver { virtual void func() = 0; };
class System1Solver : AbstractSolver { public: void func() override { }; };
class System2Solver : AbstractSolver { public: void func() override { }; };

/* 
class System1Controller { 
  public:
    System1Solver solver;
    void func() { solver.func(); };
};

class System2Controller { 
  public:
    System2Controller(System2Solver solver);
    System2Solver solver;
    void func() { solver.func(); };
}; 
*/

template <class SolverClass>
class Controller {
  public:
    SolverClass solver;
    void func() { solver.func(); };
};

class System1Controller : Controller<System1Solver> { };
class System2Controller : Controller<System2Solver> { };

auto main()->int {

    System1Controller controller1;
    System2Controller controller2;

    if (typeid(controller1) != typeid(controller2)) {
        std::cout << "Yay!" << std::endl;
    } else {
        std::cout << "Boo." << std::endl;
    }

};

另一种选择是使用多态性:在单个 Controller class 中保留指向您的 AbstractSolver 的指针,并将适当的具体求解器传递给控制器​​的构造函数。

这个例子确实有效。感谢大家确认元类在这里得到了适当的使用。

metaprogramming_crimes.cpp

/* Compile with g++ -O3 -std=c++14 -o crimes metaprogramming_crimes.cpp */

#include <iostream>

class AbstractSolver { virtual void func() = 0; };
class System1Solver : AbstractSolver { public: void func() override { }; };
class System2Solver : AbstractSolver { public: void func() override { }; };

/* 
class System1Controller { 
  public:
    System1Solver solver;
    void func() { solver.func(); };
};

class System2Controller { 
  public:
    System2Controller(System2Solver solver);
    System2Solver solver;
    void func() { solver.func(); };
}; 
*/

template <class SolverClass>
class Controller {
  public:
    SolverClass solver;
    void func() { solver.func(); };
};

class System1Controller : Controller<System1Solver> { };
class System2Controller : Controller<System2Solver> { };

auto main()->int {

    System1Controller controller1;
    System2Controller controller2;

    if (typeid(controller1) != typeid(controller2)) {
        std::cout << "Yay!" << std::endl;
    } else {
        std::cout << "Boo." << std::endl;
    }

};