减少彼此紧密关联的模板参数
Reduce template parameters tightly linked to each other
我有以下代码:
template<typename T1, typename T2, typename T3, typename T4>
void Func(void); // signature
具有 4 个模板参数的简单模板函数。 this的用法目前是这样的:
Func<Foo1, Foo1Helper, Foo1Client, Foo1Server>();
Func<Foo2, Foo2Helper, Foo2Client, Foo2Server>();
Func<Foo3, Foo3Helper, Foo3Client, Foo3Server>();
现在在用法示例中,Foo1Helper, Foo1Client, Foo1Server, Foo2Helper, Foo2Client, Foo2Server, Foo3Helper, Foo3Client, Foo3Server
是基于 Foo1, Foo2, Foo3
classes 生成的 classes。
我想要实现的是简化 Func
模板化函数,以便可以这样调用它:
Func<Foo1>();
无需指定生成的 classes,因为 classes 的名称绑定到原始 class.
你对我如何实现这一点有什么建议吗?
不完全清楚 generated 类 是什么意思,但是你可以为它们提供一个元函数:
// declare metafunctions
template <typename T>
struct Helper;
template <typename T>
struct Client;
template <typename T>
struct Server;
// provide implementations
template <>
struct Helper<Foo1>
{ using type = Foo1Helper; };
template <>
struct Client<Foo1>
{ using type = Foo1Client; };
template <>
struct Server<Foo1>
{ using type = Foo1Server; };
// the same for Foo2, Foo3, etc
然后在Func
中使用它们:
template <typename T>
void Func()
{
using THelper = typename Helper<T>::type;
using TClient = typename Client<T>::type;
using TServer = typename Server<T>::type;
}
另一种选择是使用宏。语法有点不同,但你可以有类似
#define PARAMS(name) name, name##Helper, name##Client, name##Server
然后你会像
一样使用它
Func<PARAMS(Foo1)>();
您可以像这样将 Foo
相关的 class 分组:
struct FooProfile {
using T = Foo1;
using Helper = Foo1Helper;
using Client = Foo1Client;
using Server = Foo1Server;
};
然后这样传递:
Func<FooProfile>();
函数实现如下所示:
template<typename Profile>
void Func() {
using T = typename Profile::T;
using Helper = typename Profile::Helper;
using Client = typename Profile::Client;
using Server = typename Profile::Server;
// stuff
}
我有以下代码:
template<typename T1, typename T2, typename T3, typename T4>
void Func(void); // signature
具有 4 个模板参数的简单模板函数。 this的用法目前是这样的:
Func<Foo1, Foo1Helper, Foo1Client, Foo1Server>();
Func<Foo2, Foo2Helper, Foo2Client, Foo2Server>();
Func<Foo3, Foo3Helper, Foo3Client, Foo3Server>();
现在在用法示例中,Foo1Helper, Foo1Client, Foo1Server, Foo2Helper, Foo2Client, Foo2Server, Foo3Helper, Foo3Client, Foo3Server
是基于 Foo1, Foo2, Foo3
classes 生成的 classes。
我想要实现的是简化 Func
模板化函数,以便可以这样调用它:
Func<Foo1>();
无需指定生成的 classes,因为 classes 的名称绑定到原始 class.
你对我如何实现这一点有什么建议吗?
不完全清楚 generated 类 是什么意思,但是你可以为它们提供一个元函数:
// declare metafunctions
template <typename T>
struct Helper;
template <typename T>
struct Client;
template <typename T>
struct Server;
// provide implementations
template <>
struct Helper<Foo1>
{ using type = Foo1Helper; };
template <>
struct Client<Foo1>
{ using type = Foo1Client; };
template <>
struct Server<Foo1>
{ using type = Foo1Server; };
// the same for Foo2, Foo3, etc
然后在Func
中使用它们:
template <typename T>
void Func()
{
using THelper = typename Helper<T>::type;
using TClient = typename Client<T>::type;
using TServer = typename Server<T>::type;
}
另一种选择是使用宏。语法有点不同,但你可以有类似
#define PARAMS(name) name, name##Helper, name##Client, name##Server
然后你会像
一样使用它Func<PARAMS(Foo1)>();
您可以像这样将 Foo
相关的 class 分组:
struct FooProfile {
using T = Foo1;
using Helper = Foo1Helper;
using Client = Foo1Client;
using Server = Foo1Server;
};
然后这样传递:
Func<FooProfile>();
函数实现如下所示:
template<typename Profile>
void Func() {
using T = typename Profile::T;
using Helper = typename Profile::Helper;
using Client = typename Profile::Client;
using Server = typename Profile::Server;
// stuff
}