- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
类型 trait 是否应该能够处理 std::vector < std::unique_ptr <int> >
之类的情况?并检测到它不是可复制的?
这是 https://ideone.com/gbcRUa 的示例(运行 g++ 4.8.1)
#include <type_traits>
#include <vector>
#include <iostream>
#include <memory>
int main()
{
// This prints 1, implying that it's copy constructible, when it's clearly not
std::cout << std::is_copy_constructible< std::vector<std::unique_ptr<int> > >::value << std::endl;
return 0;
}
如果这是 is_copy_constructible
的正确行为,有没有办法检测到复制结构是不正确的?好吧,不仅仅是让它无法编译。
最佳答案
这是因为 std::vector
的设计存在缺陷。 . std::vector
定义复制构造,即使它无法编译,并且依赖于 std::vector
的用户如果编译失败则不调用该方法。
如果类型包含在 vector
中,则另一种设计是 SFINAE 阻止方法的调用。没有复制构造函数。但是,std::vector
是在现代 SFINAE 技术开发之前设计的。
它可能会被重新安装到新的 C++ 迭代中,因为很少有代码会被破坏。不能说没有代码会破坏,因为您可以拥有依赖于 std::is_copy_constructible< std::vector< no_copy_type > >
事实的代码。是 std::true_type
, 或等效表达式,但这是一个非常奇怪的依赖关系。
除此之外,std::vector
比可以解决此问题的 SFINAE 技术更早,使用 SFINAE 这样做非常困惑(因为 SFINAE 是一种困惑的技术)。为 C++1y 提出的新概念精简版可能会使其更简洁,并且更容易包含在该语言的新迭代中。
当我有一个容器需要知道是否可以安全地复制、比较和排序包含的对象时,我的工作是专门针对 std::vector
在自定义特征类上,并依赖于包含类型上的自定义特征类的值。这是一个拼凑而成的解决方案,而且非常具有侵入性。
template<template<typename>class test, typename T>
struct smart_test : test<T> {};
template<template<typename>class test, typename T, typename A>
struct smart_test<test, std::vector<T,A>> : smart_test<T> {};
这给了我们:
template<typename T>
using smart_is_copy_constructible = smart_test< std::is_copy_constructible, T >;
与 <
类似和 ==
.当我遇到更多应该将它们的属性真正转发到它们的数据的容器类型时,我可以添加更多的特化,或者我可以编写一个更高级的 SFINAE 容器测试和特征并提取基础值类型并将问题分发给测试关于值类型。
但根据我的经验,我最终会在 std::vector
上进行这些测试。 .
请注意,由于 c++11 vector 添加了“参与重载解决”规则,这是“做 SFINAE”测试的标准。
关于c++ - 在 vector <unique_ptr> 上使用 is_copy_constructible 误报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18404108/
import lombok.Data; @Data public class Filter { private Operator operator; private Object va
我刚刚在 SonarQube 中发现了一个误导性问题,我们的代码如下(JS) {…} function test(searchQuery, role) { console.log("inputs: "
我想我发现了误报: const items = document.querySelectorAll(selectors); for (const item of items) { if(item)
这是我第一次尝试编写实现简单形式的堆栈跟踪的自定义异常类。 这是.h: class error { public: error (); error (const
我正在尝试构建我的第一个 XML 模式 validator 作为我的代码库和许多项目中的可重用组件。我花了一整天的时间尝试遵循示例并对其进行编码,现在已经启动并运行了概念验证。 唯一的问题是,它给了我
我的数据库中有一个表,其中有一个名为“tags”的字段,例如iOS、Android、Java、JavaScript 等。我想选择此表中的所有项目,这些项目与某些标签相匹配,例如 id | name |
我刚刚开始在一个现有的中型项目中试验 .NET 4 中的 CodeContracts,令我惊讶的是静态检查器向我发出有关以下代码片段的编译时警告: public class Foo { priv
我不知道为什么,但我以前没有问题,现在突然之间,我很久以前写的这个非常古老、可怕的新手程序触发了 Malwarebytes... :( 这个问题不是重复的,因为有问题的程序是在 (ANSI) C 而不
在 Ubuntu 9.10 下启动了一个 Git 存储库。终于升级到10.04。当前的 Git 是 1.7.0.4。 现在,当我编辑升级前最后更改的文件(使用 gedit)然后提交时,提交中充满了误报
在设置 mod_security 后,我收到了很多误报 [??]。我只是在检测中运行它,所以还没有问题,但是一旦我需要它上线,这些过滤器就会开始阻止请求。 恐怕我不能 100% 理解这些过滤器的重要性
引用 Valgrind 教程: Optimized code can cause valgrind to wrongly report uninitialized value errors. The
Sonar 是否有可能在分支之间传输“误报”? 这是我们的工作流程:我们在分支 1 中进行开发,我们对此分支进行 Sonar 检查,当分支 1 发布时,我们将其合并到主干中,然后我们从主干创建分支 2
我的代码库中有一个静态函数的实现,并且在运行 clang-tidy 时在它上面,我注意到当我很确定代码正确时,静态分析器指向可能的内存泄漏。 (我已经用 sanitizer 验证过)。我认为这很可能是
Coverity 标记了一个我无法理解的问题。 我有一个初始化器: 1686 arrayOfNodeIds componentRefs = (arrayOfNodeIds) { 1687 .s
react.js 发生了一些奇怪的事情 Top-Level API . 这是我的mocha 测试的摘录: it.only('should identify a ReactElement', funct
大家好,我在使用 Apple 的可达性代码时遇到了一些问题。我发现,即使设备正确连接到互联网,最初可达性代码也会发出 1 个错误通知(Networkstatus = NotReachable),然后是
我一直在尝试运行 Insure++使用一些科学代码并报告许多错误,尽管公平地说它正式不支持 K&R C,而且我不知道大量 K&R 函数对其评估过程有何影响。它正在测试的 C 和 C++ 代码正在从 W
当我尝试调用 CFileFind.FindFile(_T("D://Folder//*.txt")) 时,当唯一的文件是“foobar.txta”时,该方法返回 true . 但是,我不希望文件 fo
在我之前的问题 ( "Pylint E0202 False Positive?" ) 之后,这里是另一个问题(我想很具体) 我们正在使用模块子流程来执行子流程。 创建示例代码会产生以下结果: "Exa
我有一个包含 2,865,044 个条目和 3 级 MultiIndex 的数据框 MultiIndex.levels.names = ['year', 'country', 'productcode
我是一名优秀的程序员,十分优秀!