yo_*_*gdg 0 c++ move move-semantics c++17
我想与你分享一个我无法解决的小问题,这里是代码(仅供测试):
#include <windows.h>
#include <iostream>
#include <vector>
#include <string>
#include <utility>
#include <type_traits>
#include <sstream>
struct Procedure {
Procedure(HANDLE)
{ std::cout << "ctor w/connection: " << this << std::endl; }
~Procedure()
{ std::cout << "dtor: " << this << std::endl; }
Procedure(Procedure &&rhs) {
std::cout << "ctor w/move: " << this << std::endl;
this->m_Params = std::move(rhs.m_Params);
}
Procedure& operator= (Procedure &&rhs) {
std::cout << "operator= w/move: " << this << std::endl;
if (this != &rhs) this->m_Params = std::move(rhs.m_Params);
return *this;
}
Procedure& AppendParam(const std::string &str) {
std::cout << "appendparam: " << this << std::endl;
m_Params.push_back(str);
return *this;
}
void Execute( const std::string &str) {
std::stringstream ss;
ss << str << '(';
for (int i = 0, mx = m_Params.size(); i < mx; ++i) {
ss << '\'' << m_Params[i] << '\'';
if (i < mx - 1) ss << ',';
}
ss << ");";
std::cout << "calling: " << this << " : " << ss.str() << std::endl;
}
private:
Procedure(const Procedure &) = delete;
Procedure& operator=(const Procedure &) = delete;
std::vector<std::string> m_Params;
};
Procedure ProcedureCaller()
{ return Procedure(nullptr); }
int __cdecl main() {
std::cout << "test1---------------------" << std::endl; {
auto &proc = ProcedureCaller().AppendParam("param_1").AppendParam("param_2");
proc.Execute("sp_test");
}
std::cout << "test2--------------------" << std::endl; {
auto proc = ProcedureCaller();
proc.AppendParam("param_A").AppendParam("param_B");
proc.Execute("sp_test_2");
}
std::cout << "test3--------------------" << std::endl; {
ProcedureCaller().AppendParam("param_AA").AppendParam("param_BB").Execute("sp_test_2");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的结果:
test1---------------------
ctor w/connection: 00F8FC98
appendparam: 00F8FC98
appendparam: 00F8FC98
dtor: 00F8FC98
calling: 00F8FC98 : sp_test();
test2--------------------
ctor w/connection: 00F8FD70
appendparam: 00F8FD70
appendparam: 00F8FD70
calling: 00F8FD70 : sp_test_2('param_A','param_B');
dtor: 00F8FD70
test3--------------------
ctor w/connection: 004FFB20
appendparam: 004FFB20
appendparam: 004FFB20
calling: 004FFB20 : sp_test_2('param_AA','param_BB');
dtor: 004FFB20
Run Code Online (Sandbox Code Playgroud)
我有几个问题:
1-为什么"test1"的dtor在其范围结束之前被调用?我的意思是,代码甚至没有调用该Execute方法.
2-如果"test1"的dtor是一个临时对象,为什么我没有看到日志move ctor,或者至少是编译器错误,因为它试图使用已删除的copy ctor?
3-"test1"和"test2"之间的区别是什么,我希望能够以Execute任何我想要的方式调用它.
4-我错过了什么?
谢谢.
这是一个更简单的版本,演示了同样的问题:
struct X {
X() = default;
~X() { std::cout << "dtor\n"; }
X& self() { return *this; }
};
int main()
{
X& x = X().self();
std::cout << "here?\n";
}
Run Code Online (Sandbox Code Playgroud)
该程序在打印dtor之前打印here.为什么?问题是,我们有一个X()不会延长生命周期的临时(),因此它会在包含它的表达式的末尾被破坏(这是X().self()).当你得到它的引用时,它不是延长生命周期的魔法引用之一 - 你得到的只是对一个立即超出范围的对象的引用.
终身延长仅在非常有限的情况下发生.临时必须立即绑定到引用,这只能用于const引用:
X const& x = X();
std::cout << "here\n";
Run Code Online (Sandbox Code Playgroud)
现在打印here之前dtor.
此外,没有传递寿命延长.即使在最初的例子中我们做了:
X const& x = X().self();
Run Code Online (Sandbox Code Playgroud)
我们仍然会有一个悬空参考.
| 归档时间: |
|
| 查看次数: |
70 次 |
| 最近记录: |