Aleph0
Aleph0

Reputation: 6084

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!
}

Upvotes: 2

Views: 263

Answers (2)

lubgr
lubgr

Reputation: 38315

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.

Upvotes: 3

user7860670
user7860670

Reputation: 37578

The way comparator argument is accepted makes deduction problematic, so you may want to change comparator into template parameter (possibly avoiding construction of heavy std::function object ):

template<typename T> class t_EquilityComparator
{
    public: bool operator ()(const T& u, const T& v) const { return u == v; }
};

template<typename V, typename T, typename Comparator = t_EquilityComparator<T>>
bool cmpVariant(
    const V& variant,
    const T& t,
    const Comparator & equiv = Comparator{})
{
    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 equiv = [](const double& x, const double& y) { return x == y; };
    cmpVariant(number, 3.2, equiv); // This line fails to compile! Why?
}

online compiler

Upvotes: 1

Related Questions