基于类型的条件相等 operator/function

conditional equality operator/function based on type

如何根据指定的类型为 class 实现部分比较运算符(或函数)以匹配成员变量的子集?给定以下示例:

struct A { int a; bool operator==(A rhs) const { return a==rhs.a; } };
struct B { int b; bool operator==(B rhs) const { return b==rhs.b; } };
struct C { int c; bool operator==(C rhs) const { return c==rhs.c; } };
struct D { int d; bool operator==(D rhs) const { return d==rhs.d; } };

class X
{
public:
    X(int a=0, int b=0, int c=0, int d=0)
    : _a{a}, _b{b}, _c{c}, _d{d}
    {}

    A _a;
    B _b;
    C _c;
    D _d;
};

我想添加支持,以便用户可以根据 X 成员的子集比较两个 X 实例;即类似:

X x1 (1,2,3,4);
X x2 (1,1,2,3);

match<A,B,C,D>( x1, x2 ); /* should return x1._a==x2._a && ... && x1._d==x2._d */
match<A,B,C>( x1, x2 );   /* should return x1._a==x2._a && ... x1._c==x2._c */
match<A,B>( x1, x2 );     /* should return x1._a==x2._a && x1._b==x2._b */
match<A>( x1, x2 );       /* should return x1._a==x2._a */
match<A,D>( x1, x2 );     /* should return x1._a==x2._a && x1._d==x2._d */

然而以下失败

template<typename T>
bool match(X x1, X x2) { return false; }

template<>
bool match<A>(X x1, X x2) { return x1._a == x2._a; }

template<>
bool match<B>(X x1, X x2) { return x1._b == x2._b; }

template<>
bool match<C>(X x1, X x2) { return x1._c == x2._c; }

template<>
bool match<D>(X x1, X x2) { return x1._d == x2._d; }

template<typename T, typename... Args>
bool match(X x1, X x2)
{ return match<T>(x1, x2) && match<Args...>(x1, x2); }

有错误信息(*)

vard.cc: In function ‘int main()’:
vard.cc:49:35: error: call of overloaded ‘match(X&, X&)’ is ambiguous
     std::cout << match<A>( x1, x2 ) << "\n" ;
                                   ^
vard.cc:25:6: note: candidate: bool match(X, X) [with T = A]
 bool match<A>(X x1, X x2) { return x1._a == x2._a; }
      ^
vard.cc:37:6: note: candidate: bool match(X, X) [with T = A; Args = {}]
 bool match(X x1, X x2)
      ^
vard.cc: In instantiation of ‘bool match(X, X) [with T = A; Args = {B, C, D}]’:
vard.cc:46:41:   required from here
vard.cc:38:18: error: call of overloaded ‘match(X&, X&)’ is ambiguous
 { return match<T>(x1, x2) && match<Args...>(x1, x2); }
                  ^
vard.cc:25:6: note: candidate: bool match(X, X) [with T = A]
 bool match<A>(X x1, X x2) { return x1._a == x2._a; }
      ^
vard.cc:37:6: note: candidate: bool match(X, X) [with T = A; Args = {}]
 bool match(X x1, X x2)
      ^
vard.cc: In instantiation of ‘bool match(X, X) [with T = A; Args = {B, C}]’:
vard.cc:47:39:   required from here
vard.cc:38:18: error: call of overloaded ‘match(X&, X&)’ is ambiguous
 { return match<T>(x1, x2) && match<Args...>(x1, x2); }
                  ^
vard.cc:25:6: note: candidate: bool match(X, X) [with T = A]
 bool match<A>(X x1, X x2) { return x1._a == x2._a; }
      ^
vard.cc:37:6: note: candidate: bool match(X, X) [with T = A; Args = {}]
 bool match(X x1, X x2)
      ^
vard.cc: In instantiation of ‘bool match(X, X) [with T = A; Args = {B}]’:
vard.cc:48:37:   required from here
vard.cc:38:18: error: call of overloaded ‘match(X&, X&)’ is ambiguous
 { return match<T>(x1, x2) && match<Args...>(x1, x2); }
                  ^
vard.cc:25:6: note: candidate: bool match(X, X) [with T = A]
 bool match<A>(X x1, X x2) { return x1._a == x2._a; }
      ^
vard.cc:37:6: note: candidate: bool match(X, X) [with T = A; Args = {}]
 bool match(X x1, X x2)
      ^
vard.cc:38:44: error: call of overloaded ‘match(X&, X&)’ is ambiguous
 { return match<T>(x1, x2) && match<Args...>(x1, x2); }
                                            ^
vard.cc:28:6: note: candidate: bool match(X, X) [with T = B]
 bool match<B>(X x1, X x2) { return x1._b == x2._b; }
      ^
vard.cc:37:6: note: candidate: bool match(X, X) [with T = B; Args = {}]
 bool match(X x1, X x2)
      ^

为什么调用不明确?什么是正确、清晰的实施?此功能能否合并到 class 的等式运算符中?

(*)编译的测试程序只是上面代码的串联;

#include <iostream>

struct A { int a; bool operator==(A rhs) const { return a==rhs.a; } };
struct B { int b; bool operator==(B rhs) const { return b==rhs.b; } };
struct C { int c; bool operator==(C rhs) const { return c==rhs.c; } };
struct D { int d; bool operator==(D rhs) const { return d==rhs.d; } };

class X
{
public:
    X(int a=0, int b=0, int c=0, int d=0)
    : _a{a}, _b{b}, _c{c}, _d{d}
    {}

    A _a;
    B _b;
    C _c;
    D _d;
};

template<typename T>
bool match(X x1, X x2) { return false; }

template<>
bool match<A>(X x1, X x2) { return x1._a == x2._a; }

template<>
bool match<B>(X x1, X x2) { return x1._b == x2._b; }

template<>
bool match<C>(X x1, X x2) { return x1._c == x2._c; }

template<>
bool match<D>(X x1, X x2) { return x1._d == x2._d; }

template<typename T, typename... Args>
bool match(X x1, X x2)
{ return match<T>(x1, x2) && match<Args...>(x1, x2); }

int main()
{
    X x1 (1,2,3,4);
    X x2 (0,1,2,3);
    X x3 (3,3,3,3);

    std::cout << match<A,B,C,D>( x1, x2 ) << "\n" ;
    std::cout << match<A,B,C>( x1, x2 ) << "\n" ;
    std::cout << match<A,B>( x1, x2 ) << "\n" ;
    std::cout << match<A>( x1, x2 ) << "\n" ;

    return 0;
}

使用 clang++ 3.7.0 编译(g++ (GCC) 5.3.1 给出几乎相同的错误)。

如果你会使用 C++14,那么使用 std::tuple 就很容易了。

首先,我们在X中添加一个tie方法来绑定所有成员:

class X
{
public:
    X(int a=0, int b=0, int c=0, int d=0)
    : _a{a}, _b{b}, _c{c}, _d{d}
    {}

    A _a;
    B _b;
    C _c;
    D _d;

    std::tuple<A,B,C,D> tie () { return std::tie(_a,_b,_c,_d); }
};

然后我们可以从该元组中提取我们为 match 传递的类型并比较它们:

template<typename... Args>
bool match(X x1, X x2)
{ 
    return std::make_tuple(std::get<Args>(x1.tie())...) == 
           std::make_tuple(std::get<Args>(x2.tie())...); 
}

这里是一个思路,用std::tuple提供所有实际的产品操作。我们只需要公开 class 成员:

struct A { int a; bool operator==(A rhs) const { return a==rhs.a; } };
struct B { int b; bool operator==(B rhs) const { return b==rhs.b; } };
struct C { int c; bool operator==(C rhs) const { return c==rhs.c; } };
struct D { int d; bool operator==(D rhs) const { return d==rhs.d; } };

class X
{
    template <typename> friend struct Get;

public:
    X(int a=0, int b=0, int c=0, int d=0)
    : _a{a}, _b{b}, _c{c}, _d{d}
    {}

    A _a;
    B _b;
    C _c;
    D _d;
};

template <typename> struct Get;

template <> struct Get<A> { static const A & get(const X & x) { return x._a; } };
template <> struct Get<B> { static const B & get(const X & x) { return x._b; } };
template <> struct Get<C> { static const C & get(const X & x) { return x._c; } };
template <> struct Get<D> { static const D & get(const X & x) { return x._d; } };

#include <tuple>

template <typename ...Args> bool Match(const X & lhs, const X & rhs)
{
    return std::tie(Get<Args>::get(lhs)...) == std::tie(Get<Args>::get(rhs)...);
}

用法:

#include <iostream>

int main()
{
    X x1 (1,2,3,4);
    X x2 (1,1,2,3);

    std::cout << Match<A, A, A>(x1, x2) << "\n";
    std::cout << Match<A, D>(x1, x2) << "\n";
}