- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何根据指定类型为类实现部分比较运算符(或函数),以匹配成员变量的子集?给定以下示例:
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/
也许我在 Java 上工作的时间太长而没有真正理解它的一些基础知识。 我确实理解 == 用于对象引用相等,而 .equals() 用于对象值相等。 比较整数: Integer x = 1, y = 1
我是从一道考试题中得出这个答案的,但无法理解该解决方案的工作原理。如果值“x”和“y”相等,则此函数应该返回“true”,否则返回 False。 解决方法: function equal_boolea
我将带有表情符号的文本存储在 mysql 数据库中。 数据库、表和列设置为使用utf8mb4和utf8mb4_unicode_ci。 我可以毫无问题地输入单元格值(数据类型是 VARCHAR)。 但是
如果两个 DateTime 对象具有相同的日、月和年,我该如何比较?问题是他们有不同的小时/分钟/秒。 最佳答案 对于 DateTime 对象,没有好的方法可以做到这一点。所以你必须做,比方说,不是那
我一直想知道这个问题,所以我想我会问的。 您将看到的大多数地方都使用相同的语义逻辑来覆盖 Equals 和 GetHashCode 以实现成员平等...但是它们通常使用不同的实现: publi
苹果 CoreGraphics.framework , CGGeometry.h : CG_INLINE bool __CGSizeEqualToSize(CGSize size1, CGSize s
在最新的python 版本中, dict 保留了插入的顺序。在平等方面是否有任何变化。例如,目前以下工作。既然广告顺序很重要, future 会不会发生这种变化? 我问是因为有根本性的变化 - 以前
class VideoUserModel(models.Model): user = models.ManyToManyField(get_user_model()) viewlist
我在 COQ 中有一个有限枚举类型(比如 T),我想检查元素是否相等。这意味着,我需要一个函数 bool beq_T(x:T,y:T) 我设法定义这样一个函数的唯一方法是逐个分析。这会导致很多匹配语
我在 Windows 7(32 位)下的 MinGW 中使用 gfortran 来编译 Fortran 代码。这是文件 testequal.f 中包含的最少代码: program test
我有以下 jsp 片段: ${campaign.moderated}
我想检查两个稀疏数组是否(几乎)相等。而对于 numpy 数组,你可以这样做: import numpy as np a = np.ones(200) np.testing.assert_array_
我有以下类(class): public class MyDocuments { public DateTime registeredDate; public
这个问题已经有答案了: Is floating point math broken? (33 个回答) 已关闭 5 年前。 我在这里想做的是,我采用一个精度值(小于 1)并打印 1/n 类型的所有数字
我正在为我的arduino写一个草图,我想检查我的字符串的最后一个字符。 例如: 如果输入是 cats- 我想看看最后一个字符(在我的例子中是“-”)实际上是否 - 我使用的代码: 串行事件函数 vo
让我们开始: using System; public class Program { class A { public virtual void Do() { }
我只需要根据几个键(不是全部)来确定两个 HashMap 的相等性 除了单独访问每个字段并比较相等性之外,还有其他节省时间的方法吗? 最佳答案 我能想到的一种方法是在您的 HashMap 上存储某种“
在Java中,大写的Double可以为null。 但是如果我有 double a 和 b 并且我这样做: if (a.equals(b)) 如果其中之一为空,它会崩溃。有没有更好的方法来比较它们? 最
我正在尝试从我的旧数据库中插入表格数据。 Id 在数据库表和选择特定列中都相等。这是我的数据库。 旧数据库:sch -> 旧表:product (id, tag, url) (13, red, aaa
我正在开发一个应用程序,它在我的主视图中有一个侧边栏和两个 div。我试图在容器内平均分割两者的高度。我试过 height = 50% 但效果不太好。
我是一名优秀的程序员,十分优秀!