#include <iostream>
#include <memory>
#include <set>
using namespace std;
template<typename T>
class TBlotchFromWeakPtr
{
private:
const std::weak_ptr<T> _valuePtr;
public:
const std::shared_ptr<T> get() const {std::shared_ptr<T> valuePtr(_valuePtr); return valuePtr;};
TBlotchFromWeakPtr(const std::weak_ptr<T> value):_valuePtr(value){}
bool operator < (const TBlotchFromWeakPtr &other) const
{
if (_valuePtr.expired()) return false; //если обьект уже сдох мы вполне можем иметь в sete две ссылки на него при извлечении
if (other._valuePtr.expired()) return false;//из сета прибьем их и нет проблем
std::shared_ptr<T> left(_valuePtr);
std::shared_ptr<T> right(other._valuePtr);
return left<right;
}
};
int main() {
std::shared_ptr< int > p(new int());
*p=5;
std::shared_ptr< int > p1(new int());
*p1=10;
auto setWeakPtrForint=std::set<TBlotchFromWeakPtr<int> >();
std::weak_ptr<int> weakPtrForint(p);
std::weak_ptr<int> weakPtrForint1(p1);
setWeakPtrForint.insert(TBlotchFromWeakPtr<int>(weakPtrForint));
setWeakPtrForint.insert(TBlotchFromWeakPtr<int>(weakPtrForint));
setWeakPtrForint.insert(TBlotchFromWeakPtr<int>(weakPtrForint1));
for (auto it=setWeakPtrForint.begin(); it!=setWeakPtrForint.end(); ++it)
std::cout << ' ' << *(it->get());
return 0;
}