C ++中的模板化类分配

时间:2019-05-23 22:59:02

标签: c++ templates

这可能是一个非常基本的问题,但是我搜索了却找不到任何东西。

我有一些看起来像这样的代码:

if(var == 1) {
  MyClass<A> x();
  x.doThing1();
  x.doThing2();
  x.doThing3();
  ...
  x.doThing10();
} else if(var == 2) {
  MyClass<B> x();
  x.doThing1();
  x.doThing2();
  x.doThing3();
  ...
  x.doThing10();
} else {
  MyClass<C> x();
  x.doThing1();
  x.doThing2();
  x.doThing3();
  ...
  x.doThing10();
}

我想做的是减少行数,例如:

// obviously non-working code
MyClass<auto> x;
if(var == 1) {
  x<A>();
} else if(var == 2) {
  x<B>();
} else {
  x<C>();
}
x.doThing1();
x.doThing2();
x.doThing3();
...
x.doThing10();

像这样可能吗?

2 个答案:

答案 0 :(得分:1)

好吧,您可以将其重构为类似的内容:

template <class T>
void doThings()
{
  MyClass<T> t;
  t.doThing1();
  // ...
  t.doThing10();
}

// dispatch
switch (var)
{
case 1: doThings<A>(); break;
case 2: doThings<B>(); break;
// ...
}

答案 1 :(得分:1)

您可以做一些事情。

在一般情况下,如果您不介意动态内存分配,则可以使用template参数中的所需内容,在虚拟基类中定义接口,在MyClass中实现接口。遵循以下原则:

#include <memory>
#include <iostream>

class Base
{
public:
    virtual void doThing1() const = 0;
    virtual void doThing2() const = 0;
    virtual void doThing3() const = 0;
};

template <class T>
class MyClass: public Base
{
public:
    void doThing1() const {std::cout << T::foo << ".doThing1()" << std::endl;}
    void doThing2() const {std::cout << T::foo << ".doThing2()" << std::endl;}
    void doThing3() const {std::cout << T::foo << ".doThing3()" << std::endl;}
};
struct A {static const char foo = 'A';};
struct B {static const char foo = 'B';};
struct C {static const char foo = 'C';};

std::unique_ptr<Base> makeMyClassInstance(const int var)
{
    switch (var)
    {
        case 1: return std::make_unique<MyClass<A> >();
        case 2: return std::make_unique<MyClass<B> >();
        case 3: return std::make_unique<MyClass<C> >();
        default: throw std::runtime_error("unsupported");
    }
}
int main()
{
    int var = 1;
    const auto x = makeMyClassInstance(var);
    x->doThing1();
    x->doThing2();
    x->doThing3();
}

如果var不是变量,但可以在编译时知道,则可以进行更彻底的设计。在这种情况下,我将跳过MyClass,但是您会明白的:

#include <type_traits>
#include <iostream>
#include <tuple>

struct A{void foo() {std::cout << "A" << std::endl;}};
struct B{void foo() {std::cout << "B" << std::endl;}};
struct C{void foo() {std::cout << "C" << std::endl;}};

template <int I>
struct SelectType
{
    typedef typename std::remove_reference<decltype(std::get<I-1> (std::make_tuple(A(), B(), C())))>::type type;
};

template <int I, class T = typename SelectType<I>::type>
T makeMyClassInstance() {return T();}

int main()
{
    auto x1 = makeMyClassInstance<2>();
    x1.foo(); 
}