Class 将 std::chrono::duration 存储为成员?

Class that stores a std::chrono::duration as a member?

我想创建一个 class who's construct 采用 std::chrono::duration 参数并将结果存储在一个成员中,以便我稍后可以将其传递给 std::this_thread::sleep_for()

我知道我可以写一些像 sleep_for 一样工作的函数模板,如下所示:

template <typename Rep, typename Period>
void mySleep( std::chrono::duration<Rep, Period> time )
{
  std::this_thread::sleep_for(time);
}

这可能是 class 的成员函数。但是下面的情况呢?

class UsesDuration
{
public:
   template <typename Rep, typename Period>
   UsesDuration( std::chrono::duration<Rep, Period> dur ) :
      my_duration(dur) { }

   void doSomethingPeriodic()
   {
       while( some_condition )
       {
          std::this_thread::sleep_for(my_duration);
          somethingInteresting();
       }
   }    

private:
   ???  my_duration;   /* How do I declare this??? */
}

有没有一种干净的方法来保持持续时间 "abstract" A) 理​​想情况下 w/o 将整个 class 变成模板化的 class, B) 通过将 class 到 class 模板?

我能看到的一个解决方案是在你 class 中使用特定的持续时间,然后你可以使用 std::chrono::duration_cast 从提供给构造函数的类型转换为你使用的类型作为 class 成员。这允许您不模板 class 但仍然接受任何类型的持续时间

template <typename Rep, typename Period>
   UsesDuration( std::chrono::duration<Rep, Period> dur ) :
      my_duration(std::chrono::duration_cast<decltype(my_duration)>(dur)) { }

一个更简单的解决方案是只使用比您想要的更好或更好的 std::chrono::duration

#include <chrono>
#include <thread>

class UsesDuration
{
public:
   UsesDuration( std::chrono::nanoseconds dur ) :
      my_duration(dur) { }

   void doSomethingPeriodic()
   {
       while( some_condition )
       {
          std::this_thread::sleep_for(my_duration);
          somethingInteresting();
       }
   }

   void somethingInteresting();

private:
   std::chrono::nanoseconds  my_duration;
};

int
main()
{
    using namespace std::chrono_literals;
    UsesDuration x{5min};
}

没有必要对所有内容进行模板化,除非您确实想要那种普遍性。您所有的预定义单位都隐式转换为 nanoseconds。如果您遇到客户端发送的内容无法 完全 转换为 nanoseconds 的状态,您将在 compile-time 和 然后您可以决定是否要模板化,或者可能采用其他解决方案。

另一种比存储 double 更好的解决方案是存储 double-based 持续时间:

#include <chrono>
#include <thread>

class UsesDuration
{
public:
   UsesDuration( std::chrono::duration<double> dur ) :
      my_duration(dur) { }

   void doSomethingPeriodic()
   {
       while( some_condition )
       {
          std::this_thread::sleep_for(my_duration);
          somethingInteresting();
       }
   }

   void somethingInteresting();

private:
   std::chrono::duration<double>  my_duration;
};

int
main()
{
    using namespace std::chrono_literals;
    UsesDuration x{5min};
}

Every chrono::duration 将隐式转换为 floating-point 持续时间。在此示例中,我选择秒作为精度,并选择双精度作为表示形式。你可以选择任何你想要的(长双精度和微秒,等等)。

这里有很多选择。所有这些都为您提供了裸机 double 不会提供的类型安全性,并且不会牺牲性能和灵活性。

Just use double to store seconds

是你能得到的最糟糕的建议。 学习<chrono>。它具有很大的灵活性,并且有很多不需要模板。


我坚持让你学习 <chrono> 而不给你一些指导可能是不礼貌的。

Nicolai M. Josuttis' "The C++ Standard Library - A Tutorial and Reference, 2nd Edition"<chrono> 有很好的介绍。仅这一章就可以支付本书的费用。确保您获得第 2 版。他的第一版涵盖了 C++03,其中没有 <chrono>。披露:虽然他是我的朋友,但我没有任何安排(财务或其他方面)来宣传 Nico 的书。

对于那些愿意深入研究委员会文件的人,<chrono> 提议在这里:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2661.htm

duration 部分(链接在内容中)是我建议您在不耐烦的情况下开始的地方。它读起来更像是一篇教程,而不是一篇技术论文,因为我正试图让委员会自己跟上进度。它包括我在上面的回答中给出的建议,以及更多。

这是一个视频介绍教程:

https://www.youtube.com/watch?v=P32hvk8b13M