enum ABC
{
A, B, C
}
enum XYZ
{
X, Y, Z
}
template<XYZ xyz>
void DoSomething ();
template<ABC abc, XYZ xyz>
void DoSomething ();
template<> void DoSomething <X>()
{ ... }
template<> void DoSomething <Y>()
{ ... }
template<> void DoSomething <Z>()
{ ... }
By switch i have done this
template<ABC abc, XYZ xyz>
void DoSomething ()
{
switch (xyz)
{
case X: ... break;
case Y: ... break;
case Z: ... break;
default: break;
}
}
但我想做这样的事情 为第二个参数的每个枚举值编写 3 个不同的函数 并移除开关
template<> void Pos::DoSomething <ABC abc, X>()
{
...
}
template<> void Pos::DoSomething <ABC abc, Y>()
{
...
}
template<> void Pos::DoSomething <ABC abc, Z>()
{
...
}
这是怎么做到的??? 函数的模板部分特化? 请帮助我
最佳答案
没有部分功能特化。对于这种情况,您可以使用具有静态函数的结构。
template<ABC, XYZ> struct do_helper;
template<ABC abc> struct do_helper<abc, X>
{
static void apply()
{
}
};
// same for Y, Z
template<ABC abc, XYZ xyz>
void Pos::DoSomething()
{
do_helper<abc, xyz>::apply();
}
关于在 C++ 中具有多个枚举参数的 C++ 函数模板部分特化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17611466/