- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
可以有一个强制执行以下语义的函数参数:
参数不会被函数改变。调用函数永远不会为参数创建拷贝或临时对象。
例子:
void f(const std::string & str);
接口(interface)告诉客户端参数不会被改变,如果参数已经是 std::string 类型,则不会创建拷贝。
但还是可以这样称呼
const char * hello = "hello";
f(hello);
在进入函数 f 之前创建一个临时 std::string 对象,并在退出 f 后再次销毁它。
是否可以通过不同的函数声明或通过(假设地)改变 std::string 实现来禁止这一点。
最佳答案
我将从这里开始。
or by (hypothetically) altering
std::string
implementation
不要这样做,怕鼻魔。说真的,不要。让标准库以标准方式运行。
Is it possible to disallow this, either with a different function declaration
事实上,另一个声明只是门票。添加一个接受右值引用的重载,并将其定义为已删除:
void f(std::string && str) = delete;
重载解析会选择它作为临时对象,删除的定义会导致错误。现在只能使用左值调用您想要的函数。
关于c++ - const 引用函数参数 : Is it possible to disallow temporary objects?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50211811/
今天想测试一下PostgreSQL 9.1的一个新特性,就是unlogged table,结果报如下Error,谁能帮帮我? mydb=> CREATE UNLOGGED table testu (a
struct F { private: int* data; public: F( int n ) { data = new int; *dat
我创建了几个中间件类,现在想根据一些真实的 View 来测试它们。由于我的应用程序不包含任何 View 或 url conf 本身,我想知道是否有一种方法可以临时创建 View 和 url 配置,仅用
我正在使用 Spark,我想知道:如何通过对表 A 和 B 执行 sql 查询来创建名为 C 的临时表? sqlContext .read.json(file_name_A) .creat
我最近重新安装了 ubuntu 20.04.3,在做了一些设置后,我现在在运行 apt update 时总是收到以下错误.我一定是不小心删除了某种缓存文件或目录,但我不知道如何诊断或解决这个问题。 任
我的 Magento 版本是 2.2.3。我们使用的是第二代 Google Cloud SQL 实例。 运行索引命令时遇到错误 General error: 1787 Statement violat
此代码用于使用早期的 g++ 版本进行编译,例如5.3.使用 10.2 我得到以下结果(使用编译器选项 -std=c++11) tmp1.cpp: In function ΓÇÿint main(in
有没有办法不使用临时“结果”来编写以下代码? result = func(); if(result == -1) return func2(); else return res
我创建一个临时 NSManagedObject 并将其与主 NSManagedObjectContext 关联。我需要能够将其视为上下文中功能齐全的对象(执行获取请求等),因此无法在没有关联上下文的情
我一直在努力争取在 Azure 中实现角色内的同地缓存(非专用)。 在我的 MVC WebApi 2 项目的 web.config 中: ...
我写了一个类来使用它作为一个方便的 View ,例如基于范围的for s。总的来说,它只是一对带有边界检查的迭代器: template class Range { private: I begin
在运行我的代码(使用 gfortran 编译)时,我收到 Fortran 运行时警告“创建了临时数组”,我想知道是否有更好的方法来解决此警告。 我原来的代码是这样的: allocate(flx_est
经过长时间的研究,我正在写这篇文章,但我真的很难找到解决我问题的最佳方法。 我对 resKit 和 CoreData 都很陌生......无论如何,我正在映射并保存我从 Web 服务接收到的 JSON
我需要在java spring应用程序中实现像临时存储这样的东西。例如,当事件发生时,我放了一个值,但同时这个类正在被另一个监听器类监听,并且一旦需要的值到达此存储,监听器就会调用某些操作。在java
如果你查看MySql临时表的官方文档: http://dev.mysql.com/doc/refman/5.1/en/internal-temporary-tables.html 给出的理由是:
这个问题有点棘手。我正在使用将结果插入 DOM 的第三方库。 示例: $('#puthere').thirdpartyplugin(); 这将调用 thirdpartyplugin 并操作 HTML
这个查询非常简单,我想做的就是获取给定类别中按 last_updated 字段排序的所有文章: SELECT `articles`.* FROM `articles`, `ar
我已经制作了这个 C++ 代码: std::string const & Operand::toString() const { std::ostringstream convert
MySQL 文档说:“您不能在同一个查询中多次引用一个临时表。” 我知道之前有人问过这个问题。但我找不到针对以下内容的具体解决方案。 我正在对临时表进行预选 CREATE TEMPORARY TABL
我对使用 git 比较陌生。 最近我们从 master 分支了 +-10 个功能分支。我们称它们为 A、B、C 等。 我想将这些全部 merge 在一起进行测试。如果我创建一个新分支并 merge 我
我是一名优秀的程序员,十分优秀!