在运行时选择合适的专用模板
choosing appropriate specialized template at runtime
我正在使用第三方库中的一个类,它看起来像
template <typename A = DefaultT, typename B = DefaultT, typename C = DefaultT, typename D = DefaultT, typename E = DefaultT, typename F = DefaultT>
class Vertex {};
我想在运行时根据条件使用这个类的部分专用化,例如
class MyA {};
class MyB {};
class MyC {};
class MyD {};
bool useA, useB, useC, useD; //these booleans change at runtime
// I want to specialize Vertex depending on the above booleans
// The below line shouldn't compile, this is just to give an idea
typedef typename Vertex <useA ? MyA : DefaultT, useB ? MyB : DefaultT,
useC ? MyC : DefaultT, useD ? MyD : DefaultT> MyVertex;
我想有条件地选择要专门化的模板参数。我不确定这个问题是否有一个术语,我怀疑它是否有多重调度的味道。
一个简单的方法是写15(2^4-1)类,比如
typedef typename Vertex <MyA> MyVertexWithA;
typedef typename Vertex <DefaultT, MyB> MyVertexWithB;
typedef typename Vertex <MyA, MyB> MyVertexWithAB; //and so on...until
typedef typename Vertex <MyA, MyB, MyC, MyD> MyVertexWithABCD;
问题变得更加复杂,因为我必须使用一个"网格"类,该类使用专门的顶点类
template <typename VertexClass, typename Others>
class Mesh {};
现在,如果我写15个类,那么我必须为每种不同的网格类型再写15行。在使用Mesh类的地方,它变得越来越复杂。
我坚信这必须由我或编译器来完成。我的问题:
- 我想知道是否有办法让编译器为我做这件事
- C++11是否有更好的机制来处理这种情况?谢谢
答案是否定的。如果条件在运行时发生变化,则不能基于这些条件专门化/实例化模板,它们需要是常量表达式(毕竟,编译器在编译器时执行这项工作,在程序开始运行之前,所以使用运行时表达式是否定的)。如果它们是在编译时确定的,那么您可以将constexpr
技巧与std::conditional
结合使用。
正如@Karloy Horvath所提到的,你也可以做一些名为标签调度的事情,类似于下面的例子:
#include <iostream>
struct tag_yes{};
struct tag_no{};
template <typename T> void f_tag();
template <>
void f_tag<tag_yes>() // specializations
{
std::cout << "YES" << std::endl;
}
template <>
void f_tag<tag_no>()
{
std::cout << "NO" << std::endl;
}
void f(bool condition)
{
if(condition)
f_tag<tag_yes>(); // call the YES specialization
else
f_tag<tag_no>(); // call the NO specialization
}
int main()
{
bool condition = true;
f(condition); // dispatch to the "right" template function
}
或者,您甚至可以推导标签(这就是标准C++算法与各种迭代器类型一起工作的方式)
#include <iostream>
#include <type_traits>
struct Foo
{
using tag = std::true_type;
};
struct Bar
{
using tag = std::false_type;
};
template <typename T> void f_tag();
template <>
void f_tag<std::true_type>() // specializations
{
std::cout << "YES" << std::endl;
}
template <>
void f_tag<std::false_type>()
{
std::cout << "NO" << std::endl;
}
template <typename T>
void f(const T&)
{
f_tag<typename T::tag>(); // dispatch the call
}
int main()
{
Foo foo;
Bar bar;
f(foo); // dispatch to f_tag<std::false_type>
f(bar); // dispatch to f_tag<std::true_type>
}
然而,在您的情况下,多态性可能是通过工厂方法和通用虚拟接口实现的。
"运行时的模板专用化"是绝对不可能的。编译器在编译期间处理模板,生成编译为二进制代码的类。
若编译器并没有生成它,那个么就并没有合适的二进制代码可以调用,所以在运行时不能实例化这样的类。
您需要使用运行时多态性。要么是内置的虚拟机制,要么是简单的插件。
例如,您的Vertex
具有属性InterfaceA
,并且在构造函数中,默认情况下使用实现/派生自InterfaceA
的DefaultA
。但是你可以传递一些其他的CustomA
,它也是从InterfaceA
派生而来的。这是无法绕过的。基于运行时所做的选择,您不可能拥有编译时机制的优点。
编辑:如果您拥有所有的类,并且需要在运行时选择适当的版本。那么,在Mesh
使用的顶点中存在一些公共接口是非常合乎逻辑的,因此它们应该从一个公共类派生。
因此,您需要创建一个工厂来创建一个适当类型的对象,将其转换为VertexInterface
并返回
VertexInterface makeVertex(bool useA, bool useB){
if(useA && useB) return VertexInterface<MyA, MyB>();
if(useA && !useB) return VertexInterface<MyA, DefaultT>();
if(!useA && useB) return VertexInterface<DefaultT, MyB>();
// default case
return VertexInterface<DefaultT, DefaultT>();
}
您需要工厂来处理所有(不)支持的情况。不幸的是,调度需要手动完成,这是模板和运行时之间的桥梁。
- CMake-按正确顺序将项目与C运行时对象文件链接
- 我在c++代码中生成了一个运行时#3异常
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 调用专用模板时出错"no matching function for call to [...]"
- 删除指向指针的指针是运行时错误吗
- 如何用参数值调用函数(仅在运行时已知)
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- c++中的指针和运行时错误
- 在运行时处理类型擦除的数据-如何不重新发明轮子
- 有没有一种方法可以测量c++程序的运行时内存使用情况
- 建议在运行时将带有类实例的列表从c++导入qml
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 如何在GTK程序运行时禁用屏幕保护程序/电源管理/屏幕消隐
- 在同一模拟中使用静脉和静脉_ inet内容时出现运行时错误
- 读取文件时运行时的未知行为
- 函数在Windows或Linux上运行时表现不同
- 在声明中合并两个常量"std::set"(不是在运行时)
- AWS Lambda C++运行时权限被拒绝
- 在运行时选择合适的专用模板