I’m using std::chrono
to track how much time is elapsed from the instantiation until every call to someMethod()
.
A minimal sample code looks like this:
#include <chrono>
class TestClass
{
public:
TestClass();
void someMethod();
private:
std::chrono::time_point<std::chrono::steady_clock> m_timeBegin;
};
TestClass::TestClass() :
m_timeBegin(std::chrono::steady_clock::now())
{
}
void TestClass::someMethod()
{
auto timeNow = std::chrono::steady_clock::now();
auto msSinceCreation =
std::chrono::duration_cast<std::chrono::milliseconds>(timeNow - m_timeBegin);
}
I want to get rid of the #include
in the header.
Motivation
Beside compilation and link times, my main concern is about encapsulation. Using std:chrono
is only relevant in the implementation. In order to use Testclass
there is absolutely no need to use it or even know it’s involved.
Here is some possible scenario. If someone using the TestClass
decide to use std::chrono
, he can do it without add the #include
. Then, if in the future the implementation of TestClass
change stop using std:chrono
(and in consequence removing the #include
) the other code will stop compiling with no reason.
Obviously the guy who forgot to add the include did it wrong. But also obviously this will occur we like it or not.
Additional info
As it is possible that it is relevant for certain solutions, SomeMethod()
is called frequently and performance is important in my scenario.