0

让它成为一个需要这种行为的模板:

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 作为模板参数的默认值并专门化两个模板之外,是否有更简单的方法来解决此问题?

4

1 回答 1

0

使用 C++17,您可以编写

if constexpr( std::is_same<decltype(action(el)),SomeType>::value )
    auto res = action(el);
else
{ /* do something else */ }

但我认为这种结构使用模板化函数更具可读性,您可以专门用于SomeType

template<class X>
void someFunction(const X& x) { /* standard case */ }
template<>
void someFunction(const SomeType& x) { /* SomeType case */ }

在您的循环中,您只需调用:

for(QDomElement el = list.item(i).firstChildElement(tag);
                !el.isNull(); el = el.nextSiblingElement(tag))
    someFunction(action(el));
于 2017-07-30T19:15:02.583 回答