c++ - 比较变体内容的函数无法编译

标签 c++ templates boost-variant equality-operator

在我的项目中,我详尽地使用了 boost-variant。因此,对于我的单元测试,我需要根据具有特定内容 t 的特定 T 检查变体的内容。

因此,我出于此唯一目的而设置了函数 cmpVariant 并消除单元测试中的困惑。

在某些情况下,类型T没有配备operator==,因此用户可能会传递满足EqualityCompare要求的函数> (https://en.cppreference.com/w/cpp/named_req/EqualityComparable)

现在由于某些不明原因,以下代码无法编译。它说没有匹配的功能?

Clang 6.0.1 编译器错误

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.

有人知道为什么吗?

代码

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

最佳答案

编译器无法将 lambda 与函数参数类型匹配。您可以通过显式实例化函数调用来解决此问题:

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

这显然有点啰嗦,所以这里还有另一种可能,将函数声明更改为

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. */ }

可以这样调用

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

@DanielLangr 在评论中建议的改进是使用 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. */ }

这里的一个优点是摆脱 std::function 及其通常不必要的开销。

关于c++ - 比较变体内容的函数无法编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51438506/

相关文章:

c++ - "this"是类方法中的默认参数吗?

c++ - T& 对于某些模板参数 T 意味着什么?

c++ - C++ 中上周一的时间戳

c++ - 当我们有一个模板类有模板指针时,如何使用继承

c++ - 使用额外参数 boost 变体访问者

c++ - QMessageBox addButton() 使用标准图标/显示

templates - C++ 模板别名的等价性

c++ - 使用模板派生类的虚拟模板函数访问者解决方法

c++ - 字符串到 bool 表达式不起作用 C++

c++ - boost::variant 的迭代器