Variadic templates and sizeof...() confusion

I have a recursive variadic templated method that is called from a non-recursive variadic templated method (probably not relevant but I'll mention it just in case):

template < class T, class UnaryPredicate, typename... UnaryPredicates >
static bool checkPredicate( const T obj,
                            const UnaryPredicate& p,
                            UnaryPredicates... predicates )
{
    bool output = p( obj );
    if ( output && sizeof...( UnaryPredicates ) ) {
        output = checkPredicate( obj, predicates... );  //  The problem line.
    }

    return output;
}

However when called with:

.. = checkPredicate< Sy_simObject*, VisiblePredicate< Sy_simObject* >( .. );

It gives me the following error:

error: no matching function for call to 'Sy_project::checkPredicate(Sy_simObject* const&)'

I understand that the error is telling me that UnaryPredicates is empty, and there is no overload for just T, and sure enough if I put one in it compiles fine. But I don't understand how it could have gotten that far with the sizeof...( UnaryPredicates ) check in the conditional? Surely if there were no more it would have evaluated as false and the recursion would have ended?

I can fix it by just adding the overload, by I really want to understand why it doesn't work now.

Answers


Because if(cond) { body } is a runtime if. The fact that the compiler can know before-hand at compile time that it doesn't need to branch at runtime can be used to optimize the generated code, but it must not influence whether it complains about certain parts of the code.

If the code of the body is invalid if cond is false, the compiler will complain. What you are looking for is a static if, which controls whether certain parts of your code is processed by the compiler or not. There are proposals for such a thing for the next C++ version, but current C++ does not have such a construct.


Need Your Help

JSoup over VPN/proxy

java proxy httpclient jsoup vpn

I'm trying to use JSoup to scrape some pages that are on a staging server. To view the pages on the staging server with a browser I need to be connected to a VPN.