- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我正在编写一个自定义容器并想为其提供一个 RandomAccessIterator
。
template<class T>
class MyContainer
{
public:
class RandomAccessIterator
{
/*
... operators, constructors, etc...
*/
};
RandomAccessIterator begin();
RandomAccessIterator end();
/*
... class implementation ...
*/
};
现在,如何检查我刚刚编写的迭代器是否与标准库算法 100% 兼容?一个明显的解决方案是简单地尝试一个:
MyContainer<int> list;
std::sort(list.begin(), list.end());
...并验证它是否编译并产生了预期的结果。
考虑到这一点,我不确定它是否是一个可靠的“特征符合性”测试,而这正是这个问题的意义所在。如果我忘记提供任何必需的方法/运算符,编译器会总是提示吗?如果没有,确保我的类型完全实现给定特征的最佳方法是什么?
@Edit:该帖子不再将标准库称为“STL”,正如评论中指出的那样是不正确的。
最佳答案
我会说这是可能的,基于 C++20 iterator concepts 1:
template<std::input_iterator T>
using validate_input_iterator = T;
这会编译失败:
struct meow {
struct iterator{};
private:
typedef validate_input_iterator<iterator> v; // fails here
};
但是下面的编译:
struct meow: private std::string { // string has an input iterator
// struct iterator{};
private:
typedef validate_input_iterator<iterator> v; // passes!
};
如果您真的想检查您是否正确实现了所需的功能和特征,这当然无法在编译时检查,需要编写一些测试。
请注意,概念,特别是迭代器概念,声明了句法要求和语义要求。虽然基于 requires
语法声明的语法要求可以在编译时进行静态检查,但语义要求只是该类型要遵循的“注释”,不是也不能在编译时检查。另一方面,算法和编译器决策可能基于这样一个事实,即据说实现特定概念的类型遵循其语义要求。
例如,bidirectional_iterator
定义为:
23.3.4.12 Concept bidirectional_iterator [iterator.concept.bidir]
- The
bidirectional_iterator
concept adds the ability to move an iterator backward as well as forward.template<class I>
concept bidirectional_iterator =
forward_iterator<I> &&
derived_from<ITER_CONCEPT(I), bidirectional_iterator_tag> &&
requires(I i) {
{ --i } -> same_as<I&>;
{ i-- } -> same_as<I>;
};
A bidirectional iterator
r
is decrementable if and only if there exists someq
such that++q == r
. Decrementable iteratorsr
shall be in the domain of the expressions--r
andr--
.Let
a
andb
be equal objects of typeI
.I
modelsbidirectional_iterator
only if:(3.1) If
a
andb
are decrementable, then all of the following are true:(3.1.1)
addressof(--a) == addressof(a)
(3.1.2)
bool(a-- == b)
(3.1.3) after evaluating both
a--
and--b
,bool(a == b)
is stilltrue
(3.1.4)
bool(++(--a) == b)
(3.2) If
a
andb
are incrementable, thenbool(--(++a) == b)
.
如上所示,对于据说要实现 bidirectional_iterator
的给定迭代器,项目符号 #1 可以在编译时检查,而项目符号 #2 和 #3 则不能。
1 在 C++20 之前也应该可行,以类似的方式实现您自己的 SFINAE 受限模板
关于c++ - 如何检查一个类是否完全符合给定的一组特征?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59459529/
在下面的代码中,我得到一个 uninitialized value警告,但仅限于第二个 given/when例子。为什么是这样? #!/usr/bin/env perl use warnings; u
整个“开关”功能是否已成为实验性的?在没有 Perl 的 future 版本破坏我的代码的情况下,我可以依赖其中的某些部分吗?一般来说,将稳定功能更改为实验性的政策是什么? 背景use feature
有没有办法在一个条件语句中写出如下语句? a和b不能同时等于5。 (a可以是5,b可以是5,但是a AND b不能是5) 最佳答案 正如克里斯指出的那样,您要查找的是逻辑异或,相当于逻辑不等于 !=:
我正在寻找一种算法来找到给定 n 条线段的所有交点。以下是来自 http://jeffe.cs.illinois.edu/teaching/373/notes/x06-sweepline.pdf 的伪
数组中有 N 个元素。我可以选择第一项最多 N 次,第二项最多选择 N-1 次,依此类推。 我有 K 个 token 要使用并且需要使用它们以便我可以拥有最大数量的项目。 arr = [3, 4, 8
我正在尝试修复法语文本中的语法性别,想知道是否有办法从某个词条中获取所有单词的列表,以及是否可以在此类列表中进行查找? 最佳答案 尝试: import spacy lemma_lookup = spa
我正在为 Win32 编写一个简单的自动化测试应用程序。它作为一个单独的进程运行,并通过 Windows API 访问目标应用程序。我可以阅读窗口层次结构,查找标签和文本框,并通过发送/发布消息等来单
在 nodeJs 中使用 Sequelize 时,我从 Sequelize 收到此错误,如下所示: { [SequelizeUniqueConstraintError: Validation erro
本文https://arxiv.org/pdf/1703.10757.pdf使用回归激活映射 (RAM) - 而不是类激活映射 (CAM) 来解决问题。有几篇文章描述了如何实现 CAM。但是我找不到
我正在研究 Mach 动态链接器 dyld。这个问题适用于所有 Apple 平台,但很高兴得到特定于平台的答案;我正在使用 ObjC,但如果对你有用的话,我也很乐意翻译 Swift。 The rele
我有一个包含数千个 Instagram 用户 ID 的列表。我如何获得他们的 Instagram 用户名/句柄? 最佳答案 你必须使用这个 Instagram API: https://api.ins
我在下面的代码: def main(args: Array[String]) { val sparkConf = new SparkConf().setAppName("Spark-Hbase").s
我有一个表格,其中包含从 1 到 10 的数字。(从 D2 到 M2) 假设A1中有03/09/2019 并且在B1中有06/09/2019 并且在C1中有Hello 在A 列中,我有多个系列的单词,
我想在给定服务对应的 URI 的情况下检索服务的注释(特别是 @RolesAllowed )。这是一个例子: 服务: @GET @Path("/example") @RolesAllowed({ "B
我看到 OraclePreparedStatementexecuteQuery() 表现出序列化。也就是说,我想使用相同的连接对 Oracle 数据库同时运行两个查询。然而,OraclePrepare
import java.util.Scanner; public class GeometricSumFromK { public static int geometricSum(int k,
我创建了一个抽象基类Page,它说明了如何构建动态网页。我正在尝试想出一种基于作为 HttpServletRequest 传入的 GET 请求生成 Page 的好方法。例如... public cla
我的字符串是一条短信,采用以下两种格式之一: 潜在客户短信: 您已收到 1 条线索 标题:我的领导 潜在客户 ID:12345-2365 警报设置 ID:890 短信回复: 您已收到 1 条回复 标题
我在 python 中有以下代码: class CreateMap: def changeme(listOne, lisrTwo, listThree, listFour, listfive):
这是在 Hibernate 上运行的 JPA2。 我想检索相同实体类型的多个实例,给定它们的 ID。其中许多已经在持久性上下文和/或二级缓存中。 我尝试了几种方法,但似乎都有其缺点: 当我使用 ent
我是一名优秀的程序员,十分优秀!