如何防止复制构造函数在for_each函数的末尾被调用?
How do I prevent the copy constructor being called at the end of the for_each?
在下面的代码中,我期望看到COD
,但实际输出是CODD
。我最终确定存在对复制构造函数的隐藏调用,因此输出现在是COUDD
。
虽然我已经发现了为什么要额外的析构函数调用,但我不明白为什么要生成它,这阻止了我修复它。我认为它必须是从for_each
返回一元函数,但由于我没有传递或返回值(或者我不认为我是),那么不应该调用复制构造函数。一切都应该参照。我可以使用指针,但由于test_enc
对象应该在调用for_each
期间的作用域,因此引用更好。
struct test_enc
{
test_enc(std::string& ref) : ref_(ref) {
ref_.push_back('C');
}
test_enc(const test_enc& other) : ref_(other.ref_) {
ref_.push_back('U');
}
~test_enc() {
ref_.push_back('D');
}
void operator()(const char byte) {
ref_.push_back('O');
}
test_enc& operator=(const test_enc&) = delete;
std::string& ref_;
};
TEST(CheckTestEncoderEncodesExactlyOneByte)
{
const std::string unencoded_string("M");
std::string encoded_string;
std::for_each(unencoded_string.begin(), unencoded_string.end(), test_enc(encoded_string));
CHECK_EQUAL(3U, encoded_string.size());
CHECK_EQUAL("COD", encoded_string); // get "COUDD"
}
如何调用test_enc而不需要复制构造函数?
std::for_each
按值取一元函子。因此,您无法避免复制,但是您可以通过使用std::reference_wrapper
:
#include <functional>
std::string encoded_string;
test_encoded test_str(encoded_string);
std::for_each(unencoded_string.begin(),
unencoded_string.end(),
std::ref(test_str));
尝试使用std::reference_wrapper
也许?
test_enc func(encoded_string);
std::for_each(unencoded_string.begin(), unencoded_string.end(),
std::ref(func));
包装器的行为类似于引用,但允许在需要时复制它(不影响底层对象)。
std::for_each
返回std::move(f)
。test_enc不可移动,因此返回一个副本。添加move构造函数和move赋值操作符以防止复制
相关文章:
- 如何用参数值调用函数(仅在运行时已知)
- 从python中调用C++函数并获取返回值
- 当使用通配符和null指针调用函数时,对输出的说明
- 从R调用C++函数并对其进行集成时出错
- 使用QTreeView,如何通过调用函数只突出显示特定的行/列
- 如何在qt中从另一个类调用函数
- 在 COUT 语句中使用 COUT 调用函数
- 如何从线程中的不同模块调用函数?
- C++从函数指针数组调用函数
- 当 A 在 for 循环中调用函数 B 时,如何计算函数 A 的空间复杂度?
- 如何在 C/C++ 中从外部库调用函数
- 如何使用运算符在同一行中多次调用函数
- 是否可以创建一个从不同类调用函数的线程?
- 无法为类成员调用函数
- 如何从另一个标头 c++ 调用函数
- C++有什么方法可以在既不调用函数模板也不提供其模板参数的情况下引用函数模板?
- 如何只允许在调用函数 B 后调用函数 A?
- 我可以这样调用函数吗?
- 如何在 c++ 的类中递归调用函数方法?
- 为什么在指向对象的迭代器上调用函数不允许我更改对象本身?