如何根据指定的类型为类实现部分比较运算符(或函数),以匹配成员变量的子集?给出以下示例:
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给出了几乎相同的错误。)
答案 0 :(得分:3)
这是一个想法,使用std::tuple
来提供所有实际的产品操作。我们只需暴露类成员:
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";
}
答案 1 :(得分:1)
如果您可以使用C ++ 14,使用tie
非常容易。
首先,我们向X
添加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())...);
}
传递的类型并进行比较:
xAxis = d3.svg.axis()
.scale(xScale)
.tickValues(calculateTickValue)
.tickFormat(getTickFormat);
var getTickFormat = function(date){
var diff = date.format("DD") != previousDate.format("DD");
if (diff){
previousDate = date;
return date.format("MMM Do, HH:mm");
}
return date.format(tickFormat);
}
var calculateTickValue = function(){
var startTime = scope.segment.startTime;
var endTime = scope.segment.endTime;
var tickValues = [];
var difference = moment.duration(endTime.diff(startTime));
var maximumTicks = calculateMaximumTicks();
if (difference.asHours() >= maximumTicks){
tickValues = calculateHoursTicks(startTime, endTime, maximumTicks);
tickFormat = "HH:mm";
}
else {
tickValues = caclulateMinutesTicks(startTime, endTime, maximumTicks);
tickFormat = "HH:mm";
}
previousDate = startTime;
return tickValues;
}