是否可以使用标记调度来确定返回类型

Is it possible to use tag dispatching to determine return type

本文关键字:返回类型 调度 可以使 是否      更新时间:2023-10-16

标签调度是在模板的Switch传递类型中提到的。

有可能(以及如果有可能的话如何)做这样的事情吗:

struct Tag1 {};
struct Tag2 {};
template<class T, typename R>
R get();
template<>
double get<Tag1>() {return 1.3;}
template<>
char const *get<Tag2>() {return "hello";}
double aDouble = get<Tag1>();
char const *aString = get<Tag2>();

上面的代码导致编译器抱怨对重载函数的调用不明确,但我希望最后两行能传达使用意图。

Thx

您可以使用std::enable_ifstd::is_same(C++11),或者它们的增强等价物:

template <typename Tag>
typename std::enable_if<std::is_same<Tag, Tag1>::value, double>::type get()
{ ... }
template <typename Tag>
typename std::enable_if<std::is_same<Tag, Tag2>::value, char const *>::type get()
{ ... }

具有不同数量模板参数的函数模板会相互重载,因此您不是在定义专业化,而是在定义重载。像这样的东西应该起作用:

struct Tag1 {};
struct Tag2 {};
template<class T> struct MapResult;
template<> struct MapResult<Tag1> { typedef double Result; };
template<> struct MapResult<Tag2> { typedef char const* Result; };
template<class T>
typename MapResult<T>::Result get();
template<> double get<Tag1>() {return 1.2;}
template<> char const *get<Tag2>() {return "hello";}

get的第二个模板参数无法推导,因为它只作为返回类型出现:谁能说get<Tag1>()是对get<Tag1, double>的调用,而不是例如get<Tag1, int>?如果要调用例如get<Tag1, double>(),则该调用将被解析为正确的专业化。

然而,我怀疑您并不是真的希望get是一个具有两个模板参数的函数模板:返回类型可能是第一个参数的函数。因此,我建议您这样申报get

namespace result_of {
template<typename T>
struct get;
}
template<typename T>
typename result_of::get<T>::type get();

其中CCD_ 10将是计算期望结果类型的元函数。为了更简单,我们将把所有的鸡蛋放在result_of::get篮子里,而不是专门化函数模板get

namespace result_of {
template<typename T>
struct get;
}
template<typename T>
typename result_of::get<T>::type get()
{ return result_of::get<T>::apply(); }
namespace result_of {
template<>
struct get<Tag1> {
    typedef double type;
    static type apply()
    { return 1.3; }
};
template<>
struct get<Tag2> {
    typedef const char* type;
    static type apply()
    { return "hello"; }
};
}

一般来说,专门化类模板比专门化函数模板更为常见,而且在需要专门化函数样板的情况下,通常会让函数样板将其实现完全委托给类样板,从而使其变得更简单。