gpt4 book ai didi

c++ - 基于类型的条件相等运算符/函数

转载 作者:行者123 更新时间:2023-11-30 01:45:27 28 4
gpt4 key购买 nike

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

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)
^

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

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

#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 给出几乎相同的错误)。

最佳答案

这里有一个想法,使用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";
}

关于c++ - 基于类型的条件相等运算符/函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34653270/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com