如何防止右值和左值成员函数之间的代码重复
How do I prevent code repeat between rvalue and lvalue member functions?
对于下面的程序代码,我必须在接收右值和左值引用的一对成员函数中编写相同的代码。
我的目标是只使用这对中的一个(例如,只使用接受一个的右值),其他的。我阅读了std::forward
的参考资料,据我所知,它似乎是为了这个目的。但是,当我删除使用1的左值引用时,我会得到以下编译器错误。
"TestClass::TestClass(const TestClass&)":无法将参数1从"std::wstring"转换为"std:&'
如何防止这种代码重复?
#include <iostream>
#include <string>
class TestClass
{
public:
TestClass(const std::wstring & Text)
: Text(Text)
{
std::wcout << L"LValue Constructor : " << Text << std::endl;
/*Some code here...*/
}
TestClass( std::wstring && Text)
: Text(std::forward<std::wstring>(Text))
{
std::wcout << L"RValue Constructor : " << this->Text << std::endl;
/*Same code here...*/
}
TestClass(const TestClass & Another)
: Text(Another.Text)
{
std::wcout << L"Copy Constructor : " << Text << std::endl;
/*Some code here...*/
}
TestClass( TestClass && Another)
: Text(std::forward<std::wstring>(Another.Text))
{
std::wcout << L"Move Constructor : " << Text << std::endl;
/*Same code here...*/
}
private:
std::wstring Text;
};
int wmain(int argc, wchar_t *argv[], wchar_t *envp[])
{
std::wstring Argument(L"Testing Copy");
TestClass Class1Copy(Argument);
TestClass Class1Move(L"Testing Move");
TestClass Class2Copy(Class1Copy);
TestClass Class2Move(std::move(Class1Move));
_wsystem(L"pause");
return 0;
}
输出:
LValue Constructor : Testing Copy
RValue Constructor : Testing Move
Copy Constructor : Testing Copy
Move Constructor : Testing Move
Press any key to continue . . .
如果移动构建预计非常便宜,那么您可以按价值进行移动。这正好比一对复制和移动重载多移动一次。
如果你想要最佳效率,和/或如果移动结构更便宜,但便宜 现在所有的事情都转到一个构造函数。 您甚至可以混合使用这两种技术:按值使用template<class T>
std::decay_t<T> copy(T&& t) {
return std::forward<T>(t);
}
class TestClass {
public:
TestClass(std::wstring const& Text)
TestClass( copy(Text) )
{}
TestClass(TestClass const& o)
: TestClass( o.Text )
{}
TestClass(TestClass&& o)
: TestClass( std::move(o).Text ) // pattern does the right thing more often than `std::move(o.Text)` does.
{}
// only "real" ctor:
TestClass( std::wstring&& Text)
: Text(std::forward<std::wstring>(Text))
{
std::wcout << L"RValue Constructor : " << this->Text << std::endl;
/*Code here...*/
}
// ...
std::wstring
(正如我们所知,移动起来很便宜)和为TestClass
代码做转发(或者任何不太可能稳定的代码)。
您可以按值,然后按move
。那么您只需要N
过载,而不需要2N
:
TestClass(std::wstring Text)
: Text(std::move(Text))
{
}
你可以通过什么都不写来避免复制构造函数和移动构造函数的重复;在这种情况下,编译器将默认生成它们。
我认为您无法做到这一点,因为函数的签名决定了何时何地使用它们。它就像一个copy constructor
和一个assignment operator
。它们做了一些类似的事情,但编译器会根据上下文调用相应的。
如果您想避免重复使用代码,只需将通用性考虑到一个单独的函数中即可。
- 在c代码之间共享数据的最佳方式
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- 无法理解代码背后的逻辑,这是在两个给定数字之间生成素数的优化问题
- 两个代码段之间有什么区别?
- 当我使用按引用返回时,我不知道这些代码之间的区别
- 如何在括号与代码之间添加空格以进行C++?
- 为什么这个C++程序在代码::块和在线 IDE 之间返回不同的结果?
- 如果我在下面的代码中使用 list 而不是 vector,为什么在我尝试在迭代器之间执行减法的行中编译失败?
- 在两个派生类之间执行专用代码
- 在Emscripten和Qt之间共享OpenGL代码
- 这两个代码之间的差异(为什么我的数组也有额外的空间,即使我限制了它)
- 无法将Android WebView渲染到C 和Java代码之间共享的外部纹理中
- 使用运行时参数与编译时参数在类之间共享代码
- 任何可以在单个 CPU 指令中在 0 和 1 之间翻转位/整数/布尔值的可能代码
- 为什么两个相同的代码给出不同的输出,而它们之间的唯一区别是不同的变量名称和写作样式
- 不理解连续数字之间的空格代码
- 这些代码之间的区别在哪里?输出不应该是一样的吗?
- Objective-C源代码和clang -rewrite-objc C++代码之间有什么关系?
- 框架代码和普通代码之间的区别
- 如何避免语法相同的常量和非常量函数之间代码重复,这些函数在语义上不相同