有什么方法可以解决 c++ 不支持运行时模板的事实吗?

Any way to workaround the fact that c++ doesn't support runtime's templates?

本文关键字:事实 运行时 不支持 方法 什么 c++ 解决      更新时间:2023-10-16

我有一个模板类,它有两个模板参数。

template <class T,class A>
class Z{...};

假设我有四种类型的T,和相同数量的a,所以,有16选项!

如果我想在运行时选择其中一个,我需要写一个庞大而恶心的案例。

我知道c++不支持运行时模板。

还有别的方法吗?

实现类似的一种方法是向class Z添加依赖项,并在运行时提供这些依赖项——这被称为策略模式。

当然,这意味着class Z不再被模板化了。但是真的没有办法鱼和熊掌兼得:模板是一个编译时构造,而你是在询问如何在运行时配置一些东西。您不能在运行时配置类型,只能配置对象。

不,真的没有。这是模板固有的特性,这是完全不可能的。

我并不是说这是一个好的解决方案,但我在过去看到过它的实现:您可以将模板化类的代码保存在资源文件中,并在运行时将其编译成可插入的模块 !

顺便说一下,我建议重构这个问题,这样你只需要M + N,而不是实例化MN模板;我不知道这在你的问题中是否可能,但通常是,例如,如果你从一种类型转换为另一种类型,通常可以引入一种适用于所有类型的中间表示。您仍然需要case或某种形式的多态性来处理各种类型,但这是不可避免的。

虚拟类在运行时给你模板在编译时给你的东西。种。

模板代码在编写时并不是真正的代码,只是一个可以用来生成代码的模板。每次你用它…它通常会在这里生成代码。如:

Z<int, std::string> z; // At this point a new class called Z<int, std::string> is defined, then the code to instantiate it at run time is generated.
c++标准实践:虚接口类与模板

也许这就是你想要的?类似的事情使用多态性和虚拟类…

class Z{
public:
    virtual void doSomething();
};
class ZT : public Z {
public:
    void doSomething();
};
class ZA : public Z {
public:
    void doSomething();
};
...
void useClasses(Z* ptr) {
    ZT* ztPtr = dynamic_cast<ZT*>(ptr); // Runtime conversion
    if (ztPtr) {
         // do specific stuff for this type
    } else {
         ptr->doSomething(); // generic behaviour that'll call whichever class ptr actually is
    }
}

这可能符合也可能不符合您的需求,但一个简单的通用接口听起来像是您想要的:

class Z_interface{ //abstract interface
public:
    virtual ~Z_interface() {} //virtual destructor
    virtual void func()=0; //virtual member
};
template <class T,class A>
class Z : public Z_interface { //implimentation
public:
    virtual ~Z_interface() {}
    virtual void func() {}
};
int main() {
    std::unique_ptr<Z_interface> ptr;
    switch(stuff) { //make the correct type
    case 0: ptr = std::unique_ptr<Z_interface>(new Z<int, char>()); break;
    case 1: ptr = std::unique_ptr<Z_interface>(new Z<int, short>()); break;
    case 2: ptr = std::unique_ptr<Z_interface>(new Z<long, char>()); break;
    case 3: ptr = std::unique_ptr<Z_interface>(new Z<long, short>()); break;
    };
    ptr->func(); //don't care about the type anymore, just the algorithms
}