有什么方法可以解决 c++ 不支持运行时模板的事实吗?
Any way to workaround the fact that c++ doesn't support runtime's templates?
我有一个模板类,它有两个模板参数。
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
}
相关文章:
- CMake-按正确顺序将项目与C运行时对象文件链接
- 我在c++代码中生成了一个运行时#3异常
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 删除指向指针的指针是运行时错误吗
- 如何用参数值调用函数(仅在运行时已知)
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- c++中的指针和运行时错误
- 在运行时处理类型擦除的数据-如何不重新发明轮子
- 有没有一种方法可以测量c++程序的运行时内存使用情况
- 建议在运行时将带有类实例的列表从c++导入qml
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 如何在GTK程序运行时禁用屏幕保护程序/电源管理/屏幕消隐
- 在同一模拟中使用静脉和静脉_ inet内容时出现运行时错误
- 读取文件时运行时的未知行为
- 函数在Windows或Linux上运行时表现不同
- 在声明中合并两个常量"std::set"(不是在运行时)
- AWS Lambda C++运行时权限被拒绝
- 通过switch和static_cast访问多态对象的运行时类型
- 有什么方法可以解决 c++ 不支持运行时模板的事实吗?