让它成为一个需要这种行为的模板:
template<typename MyActionLambda>
void enumerateChildrenByTag(QDomNodeList& list, const QString& tag, MyActionLambda action )
{
for(int i = 0; i < list.size(); i++) {
QDomElement el = list.item(i).firstChildElement(tag);
while(!el.isNull())
{
if( typeid(decltype(action(el))) == typeid(SomeType) )
{
auto res = action(el)
// do something with res
}
else
// do something with action(el)
el = el.nextSiblingElement(tag);
}
}
}
这显然是不可能的,因为它为 lambda 编写的返回类型为 void,因为 if() 的两个分支都应该是合法的。除了将 declspec 作为模板参数的默认值并专门化两个模板之外,是否有更简单的方法来解决此问题?