Search code examples
c++templatesboost-variantequality-operator

Function to compare contents of variant fails to compile


In my projects I'm using boost-variant exhaustively. Hence, for my unit tests I need to check the contents of a variant against a certain T with a certain content t.

So I deviced the function cmpVariant for this sole purpose and to remove clutter from my unit tests.

In some cases the type T is not equipped with an operator==, so that the user might pass a function satisfying the EqualityCompare Requirement (https://en.cppreference.com/w/cpp/named_req/EqualityComparable)

Now for some obscure reason the following code fails to compile. It says, that there is no matching function?

Clang 6.0.1 Compiler Error

prog.cc:22:5: error: no matching function for call to 'cmpVariant'
    cmpVariant(number, 3.2, lambdaEquiv); // Fails!
    ^~~~~~~~~~
prog.cc:6:6: note: candidate template ignored: could not match 'function<bool (const type-parameter-0-1 &, const type-parameter-0-1 &)>' against '(lambda at prog.cc:19:24)'
bool cmpVariant(
     ^
1 error generated.

Does anyone knows why?

Code

#include <iostream>
#include <boost/variant.hpp>
#include <functional>

template<typename V, typename T>
bool cmpVariant(
    const V& variant,
    const T& t,
    const std::function<bool(const T& u, const T& v)>& equiv = [](const T& u, const T& v) {return u == v; })
{
    if (variant.type() != typeid(t)) return false;
    auto v = boost::get<T>(variant);
    return equiv(v, t);
}

int main(int, char**) {
    boost::variant<double, int> number{ 3.2 };
    cmpVariant(number, 3.2);
    auto lambdaEquiv = [](const double& x, const double& y) { return x == y; };
    std::function<bool(const double&, const double&)> equiv = lambdaEquiv;
    cmpVariant(number, 3.2, equiv); // Works!
    cmpVariant(number, 3.2, lambdaEquiv); // Fails!
}

Solution

  • The compiler is not able to match the lambda to the function parameter type. You can fix this by explicitly instantiating the function call:

    cmpVariant<boost::variant<double, int>, double>(number, 3.2, equiv);
    

    This is clearly a bit wordy, so here is another possibility changing your function declaration to

    template<typename V, typename T, typename Fct = std::function<bool(const T& u, const T& v)>>
    bool cmpVariant(
        const V& variant,
        const T& t,
        Fct&& f = [](const T& u, const T& v) {return u == v; })
    { /* Same as before. */ }
    

    which can be called like this

    cmpVariant(number, 3.2, equiv); // Type deduction works now.
    

    An improvement suggested by @DanielLangr in the comments is to employ std::equal_to.

    template<typename V, typename T, typename Fct = std::equal_to<T>>
    bool cmpVariant(
          const V& variant,
          const T& t,
          Fct&& f = std::equal_to<T>{})
    { /* Again, same as before. */ }
    

    One advantage here is to get rid of std::function and its often unnecessary overhead.