Kos*_*Kos 9 c++ template-meta-programming variadic-templates c++11
有关:
考虑这对可变参数模板:
template<typename Dummy>
bool All(Param& c) {
return true;
}
template<typename Dummy, Func* f, Func* ...rest>
bool All(Param& c) {
return f(c) && All<Dummy, rest...>(c);
}
Run Code Online (Sandbox Code Playgroud)
这可以工作和编译.但是,如何在没有第一个模板参数的情况下编写它?
听起来微不足道?嗯,这就是我的想法.:-)让我们考虑一些想法.
template<Func* f, Func* ...rest>
bool All(Param& c) {
return f(c) && All<rest...>(c);
}
template<>
bool All(Param& c) {
return true;
}
Run Code Online (Sandbox Code Playgroud)
不会工作......当我尝试这个时,我心里有专长,但第二个想法不是它的工作原理.
在最初的例子中,我创建了两个不同的重载模板,首先采用1个模板参数,第二个采用2个或更多.没有歧义,也没有专业化.我做对了吗?
bool All(Param& c) {
return true;
}
template<Func* f, Func* ...rest>
bool All(Param& c) {
return f(c) && All<rest...>(c);
}
Run Code Online (Sandbox Code Playgroud)
不会明显工作,All<rest...>
与rest...
为空不会扩大到到非模板函数的调用.
让我们重新解决一下这个问题.
template<Func* f>
bool All(Param& c) {
return f(c);
}
template<Func* f, Func* ...rest>
bool All(Param& c) {
return f(c) && All<rest...>(c);
}
Run Code Online (Sandbox Code Playgroud)
这个是禁止的,因为所有(c)都是模棱两可的.因此我需要一个0-arg case和一个> 0-arg case ...或者1-arg case和> 1-arg case呢?
想法#3.5:
template<Func* f>
bool All(Param& c) {
return f(c);
}
template<Func* f, Func* f2, Func* ...rest>
bool All(Param& c) {
return f(c) && All<f2, rest...>(c);
}
Run Code Online (Sandbox Code Playgroud)
是的,工作,但包含copypasta(在这种情况下简单,但可能更大!),因此我说它并不比我开始时更好.另一种解决方法.
让我们尝试#1但是使用类而不是函数.
template<Func* f, Func* ...rest>
struct All {
static bool func(Param& c) {
return f(c) && All<rest...>(c);
}
};
template<>
struct All {
static bool func(Param& c) {
return true;
}
};
Run Code Online (Sandbox Code Playgroud)
这看起来很有希望,因为我可以专门化课程.但是,嘿,这是什么?
抱歉,未实现:无法将'rest ...'扩展为固定长度的参数列表
这不是GCC 4.4的事吗?我在MinGW GCC 4.6.1(tdm-1).
无论如何,我是否应该认为我不能以直截了当的方式做这么基本的事情?是否需要使用带有附加虚拟模板参数的变通方法来完成此任务?
或者是否有一个简单,正确的变体来指定零参数情况,哪个可行?
在这个问题的情况下,由于模板参数是非类型的,如果我们准备一个带有默认模板参数的函数,如下所示,则Dummy
可以保存参数:
template<typename = void>
bool All(Param& c) {
return true;
}
template<Func* f, Func* ...rest>
bool All(Param& c) {
return f(c) && All<rest...>(c);
}
Run Code Online (Sandbox Code Playgroud)
但是,我不确定这是否始终适用。对于更一般的情况,std::enable_if
或者可能需要类似的调度(但这会使代码有点冗长)。
归档时间: |
|
查看次数: |
1808 次 |
最近记录: |