在运行时选择合适的专用模板

choosing appropriate specialized template at runtime

本文关键字:专用 运行时 选择      更新时间:2023-10-16

我正在使用第三方库中的一个类,它看起来像

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类的地方,它变得越来越复杂。

我坚信这必须由我或编译器来完成。我的问题:

  1. 我想知道是否有办法让编译器为我做这件事
  2. 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,并且在构造函数中,默认情况下使用实现/派生自InterfaceADefaultA。但是你可以传递一些其他的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>();
}

您需要工厂来处理所有(不)支持的情况。不幸的是,调度需要手动完成,这是模板和运行时之间的桥梁。