Here's the code
module;
#include <iostream>
#include <chrono>
#include <vector>
export module Main;
template<class T>
class Timed final {
private:
T _element;
std::chrono::milliseconds _time;
public:
Timed(T&& element, long t) : _element(std::forward<T>(element)), _time(t) { }
~Timed() = default;
// Copy Constructor
Timed(const Timed& other) : _element(other._element), _time(other._time) { }
// Copy Assignment Operator
Timed& operator=(const Timed& other)
{
if (this != &other)
{
_element = other._element;
_time = other._time;
}
return *this;
}
// Move Constructor
Timed(Timed&& other) noexcept : _element(std::move(other._element)), _time(other._time) { }
// Move Assignment Operator
Timed& operator=(Timed&& other) noexcept
{
if (this != &other)
{
_element = std::move(other._element);
_time = other._time;
}
return *this;
}
[[nodiscard]] const T& GetElement() const noexcept
{
return _element;
}
[[nodiscard]] const std::chrono::milliseconds& GetTime() const noexcept
{
return _time;
}
};
template<typename T>
bool check_erase(std::vector<Timed<T>>& vector, const std::chrono::time_point<std::chrono::steady_clock>& startTime)
{
const auto currentTime{ std::chrono::steady_clock::now() };
const auto res{ std::erase_if(vector, [&](Timed<T>& t) -> bool
{
return std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - startTime) > t.GetTime();
}) > 0 };
if (res)
{
std::cout << "deleting\n";
}
return res;
}
export int main()
{
std::vector<Timed<int>> elements{
{ 1, 3000 },
{ 2, 1000 },
};
const auto startTime{ std::chrono::steady_clock::now() };
while (!elements.empty())
{
check_erase(elements, startTime);
}
return 0;
}
