【发布时间】:2022-06-10 17:40:45
【问题描述】:
我有这样的代码:
template<class Command>
void registerCmd() {
Command x{};
// do something with x...
}
namespace Cmd
{
struct GET { /* some methods */ };
struct GETSET { /* some methods */ };
struct DEL { /* some methods */ };
void registerCommands() {
registerCmd<GET>();
registerCmd<GETSET>();
registerCmd<DEL>();
}
}
我喜欢代码的结果。我想知道是否有办法将此代码更改为这样的:
namespace Cmd
{
void register() {
// this does not compile
for (TYPE in{ GET, GETSET, DEL })
registerCmd<TYPE>();
}
}
可能与可变参数模板一起使用?
【问题讨论】:
标签: c++ templates c++17 variadic-templates function-templates