如何构造可以调用和返回两种不同类型的模板



我正在处理一些代码,其中我有两个类,每个类都有一个返回具有特定值的所述类的方法。现在这两个类有不同的名称,并且返回具有特定值的所述类的方法具有不同的名称,但是值是相同的。

class_a a = class_a::get_value_a()
class_b b = class_b::get_value_b()
a.value() == b.value() is true

现在我正在尝试制作的是使用模板获取此值的通用方法

class_generic g_a = class_generic::get_value<class_a>();
class_generic g_b = class_generic::get_value<class_b>();
g_a.value() == g_b.value() is true

但是在制作模板时,我尝试执行以下操作

template <typename T> T class_generic::get_value()
{
if (typeid(T).hash_code() == typeid(class_a).hash_code())
{
return class_a::get_value_a()
}
if (typeid(T).hash_code() == typeid(class_b).hash_code())
{
return class_b::get_value_b();
}
}

但这不会编译,在我看来,它试图解析两种返回类型并转换一种。

那么我该如何正确执行此操作呢?

问候

您可以应用模板专用化。

例如
// primary template
template <typename T> 
T class_generic::get_value();
// explicit specialization for T = class_a
template <> 
class_a class_generic::get_value<class_a>()
{
return class_a::get_value_a();
}
// explicit specialization for T = class_b
template <> 
class_b class_generic::get_value<class_b>()
{
return class_b::get_value_b();
}

或者应用 Constexpr If(自 C++17 起(,其条件在编译时被评估,然后可以避免您的代码片段遇到的问题。

template <typename T>
T class_generic::get_value()
{
if constexpr (std::is_same_v<T, class_a>)
{
return class_a::get_value_a();
} 
else if constexpr (std::is_same_v<T, class_b>)
{
return class_b::get_value_b();
} 
else 
{
// return something else..
}
}

在当前标准中,这可以完成为

struct class_generic {
template<typename T> T get_value() {
if constexpr(std::is_same_v<T, class_a>) {
return class_a::get_value_a();
}
else return class_b::get_value_b();
}
};

请注意,如果两个备选方案不能同时编译,即代码的一部分只针对一个模板参数值编译,而另一部分代码编译另一个,则它们应该是同一if constexpr/else运算符的不同分支。

除了已经给出的正确解决方案之外,您应该注意到 typeid(( 是一个运行时运算符,用于在运行时检查多态类型,模板是否是编译时问题。因此,在代码中的模板计算期间不会计算 typeid。

最新更新