简体   繁体   English

避免在标题中包含chrono

[英]Avoid including chrono in header

I'm using std::chrono to track how much time is elapsed from the instantiation until every call to someMethod() . 我正在使用std::chrono跟踪从实例化到每次对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. 我想摆脱标题中的#include

Motivation 动机

Beside compilation and link times, my main concern is about encapsulation. 除了编译和链接时间外,我主要关心的是封装。 Using std:chrono is only relevant in the implementation. 使用std:chrono仅与实现有关。 In order to use Testclass there is absolutely no need to use it or even know it's involved. 为了使用Testclass ,绝对没有必要使用它,甚至不需要知道它所涉及的。

Here is some possible scenario. 这是一些可能的情况。 If someone using the TestClass decide to use std::chrono , he can do it without add the #include . 如果使用TestClass某人决定使用std::chrono ,则可以在不添加#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 . 然后,如果将来更改TestClass的实现停止使用std:chrono (并因此删除了#include ),则其他代码将无故停止编译。

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. 由于可能与某些解决方案有关, SomeMethod()经常调用SomeMethod()而性能在我的方案中很重要。

Your problem would be solved if time_point was a custom type, because then you could just forward declare it, change the member to a (unique) pointer, and move the include to the .cpp file. 如果time_point是自定义类型,将可以解决您的问题,因为您可以向前声明它,将成员更改为(唯一)指针,然后将include移至.cpp文件。 However, forward declaration of std types is undefined behavior, so that is not possible. 但是,前向声明std类型是未定义的行为,因此这是不可能的。 This leaves you basically with the following options: 基本上,您可以使用以下选项:

  • Wrap the std::chrono::time_point<std::chrono::steady_clock> in a custom class. 在自定义类中包装std::chrono::time_point<std::chrono::steady_clock> Then you can safely forward declare that type, change the member to a (unique) pointer, and move the include of the custom class (which includes <chrono> ) into the .cpp file. 然后,您可以安全地声明该类型,将成员更改为(唯一的)指针,然后将自定义类的包含(包括<chrono> )移动到.cpp文件中。
  • Use the pimpl idiom and move the whole implementation of your class into the .cpp file. 使用pimpl习惯用法,将类的整个实现移到.cpp文件中。

I understand your motivation. 我了解您的动机。 Still, while your scenario that someone uses <chrono> and forgets to include it is not far fetched, it is a problem of that someone, not yours. 尽管如此,虽然您的情况是有人使用<chrono>并忘记包含它,但这并不是一个牵强附会的问题,而是有人而不是您的问题。 So you should think about whether it is really necessary to introduce some complexity, just to hide the <chrono> header. 因此,您应该考虑是否真的有必要引入一些复杂性,只是隐藏<chrono>标头。 The standard library includes usually dont hurt, especially because they do not introduce rebuild impact. 标准库通常不会造成伤害,尤其是因为它们不会带来重建影响。

As others have noted, using a smart pointer for the pimpl idiom or the forward declared type would introduce another include, namely <memory> . 正如其他人指出的那样,为pimpl习惯用法或正向声明类型使用智能指针将引入另一个include,即<memory> Even though this is usually a more common header than <chrono> you just exchanged one include for another. 即使这通常是比<chrono>更常见的标头,您也只是将一个include交换为另一个。 So if you really want to avoid additional includes, you may want to use a raw pointer. 因此,如果您确实要避免其他包含,则可能需要使用原始指针。 But then you have to take care of other problems, such as copy and move operations. 但是随后您必须注意其他问题,例如复制和移动操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM