在意外的函数结束时清理,std 等效
Cleanup at unexpected function end, std equivalent
我有一些函数需要使用成员变量(自定义 classes 的向量)。
在此功能结束时,需要清除此成员,但在此功能期间它需要保留为成员。
另一个问题是,由于程序的自定义错误处理,函数可能会提前结束。但是会员还需要清零
我首先使用 std::move 在局部变量的开头移动了这个成员。
这工作得很好,但现在证明我需要变量作为成员变量保留到该函数结束。
我想出了以下解决方案,使用 unique_ptr 和一个将在销毁时移动的参考。
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
template <class T>
class Clean
{
public:
Clean(T& clean)
: m_clean(clean) {}
~Clean()
{
T destroy = move(m_clean);
}
private:
T& m_clean;
};
class A
{
public:
A()
{
m_numbers = { { 3, 1 ,4, 1, 5} };
}
void display()
{
auto cleanNumbers = make_unique<Clean<vector<int> > >(m_numbers);
for(int number: m_numbers)
cout << number << endl;
}
private:
vector<int> m_numbers;
};
int main()
{
A a;
a.display();
cout << "should be empty now" << endl;
a.display();
return 0;
}
也欢迎任何更清洁的解决方案,但我的实际问题如下。
是否有与我使用的 Clean class 等效的标准?
Ps:使用 g++ 5.3.0
为我编译的代码片段
g++ -std=c++14 -o main main.cpp
您可以使用 shared_ptr 的自定义删除器来获得相同的结果。
void A::display()
{
std::shared_ptr<int> dummy (
(int*)alloca(sizeof(int)), // very fast allocate on stack
[&](int*) { this->m_numbers.clear(); }
);
for(int number: m_numbers)
cout << number << endl;
}
这是完整的代码,编译良好,gcc 5.3 -Wall -Wpedantic -march=native -std=c++14
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
class A
{
public:
A()
{
m_numbers = { { 3, 1 ,4, 1, 5} };
}
void display()
{
std::shared_ptr<int> dummy (
(int*)alloca(sizeof(int)),
[&](int*) { this->m_numbers.clear(); }
);
for(int number: m_numbers)
cout << number << endl;
}
private:
vector<int> m_numbers;
};
int main()
{
A a;
a.display();
cout << "should be empty now" << endl;
a.display();
return 0;
}
这是我感谢评论和其他问题的结果:
void display()
{
auto cleanNumber = [](decltype(m_numbers)* numbers){
if(numbers)
numbers->clear();
};
auto pClean = std::unique_ptr<decltype(m_numbers), decltype(cleanNumber)>(&m_numbers, cleanNumber);
for(int number: m_numbers)
cout << number << endl;
}
我有一些函数需要使用成员变量(自定义 classes 的向量)。
在此功能结束时,需要清除此成员,但在此功能期间它需要保留为成员。
另一个问题是,由于程序的自定义错误处理,函数可能会提前结束。但是会员还需要清零
我首先使用 std::move 在局部变量的开头移动了这个成员。 这工作得很好,但现在证明我需要变量作为成员变量保留到该函数结束。
我想出了以下解决方案,使用 unique_ptr 和一个将在销毁时移动的参考。
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
template <class T>
class Clean
{
public:
Clean(T& clean)
: m_clean(clean) {}
~Clean()
{
T destroy = move(m_clean);
}
private:
T& m_clean;
};
class A
{
public:
A()
{
m_numbers = { { 3, 1 ,4, 1, 5} };
}
void display()
{
auto cleanNumbers = make_unique<Clean<vector<int> > >(m_numbers);
for(int number: m_numbers)
cout << number << endl;
}
private:
vector<int> m_numbers;
};
int main()
{
A a;
a.display();
cout << "should be empty now" << endl;
a.display();
return 0;
}
也欢迎任何更清洁的解决方案,但我的实际问题如下。
是否有与我使用的 Clean class 等效的标准?
Ps:使用 g++ 5.3.0
为我编译的代码片段g++ -std=c++14 -o main main.cpp
您可以使用 shared_ptr 的自定义删除器来获得相同的结果。
void A::display()
{
std::shared_ptr<int> dummy (
(int*)alloca(sizeof(int)), // very fast allocate on stack
[&](int*) { this->m_numbers.clear(); }
);
for(int number: m_numbers)
cout << number << endl;
}
这是完整的代码,编译良好,gcc 5.3 -Wall -Wpedantic -march=native -std=c++14
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
class A
{
public:
A()
{
m_numbers = { { 3, 1 ,4, 1, 5} };
}
void display()
{
std::shared_ptr<int> dummy (
(int*)alloca(sizeof(int)),
[&](int*) { this->m_numbers.clear(); }
);
for(int number: m_numbers)
cout << number << endl;
}
private:
vector<int> m_numbers;
};
int main()
{
A a;
a.display();
cout << "should be empty now" << endl;
a.display();
return 0;
}
这是我感谢评论和其他问题的结果:
void display()
{
auto cleanNumber = [](decltype(m_numbers)* numbers){
if(numbers)
numbers->clear();
};
auto pClean = std::unique_ptr<decltype(m_numbers), decltype(cleanNumber)>(&m_numbers, cleanNumber);
for(int number: m_numbers)
cout << number << endl;
}