我有一个需要下一个类型的工作模板:int,float,double,std::chrono::nanoseconds,std::chrono::milliseconds和std::chrono::seconds.
该模板有一个成员函数一起工作int,float和double但我需要编写纳秒,另一个为毫秒,另一个用于秒一个专业化.
为了得到一个有效的代码,我不得不为每个std::chrono"类型" 编写一个基本相同代码的专门化.所以我失去了使用模板的优势.
我已经读过计时器"类型"是持续时间实例化的typedef.
有没有办法只写一个纳秒,毫秒和秒的特化?
以下是我的代码的样子:
// file: myclass.hh
template<typename T>
class Myclass {
public:
// Variable (I want to initialize m_max to the maximum value possible for the
// T type)
T m_max ;
// Constructor
Myclass ( ) ;
};
// file: myclass.cc
#include <limits>
#include <chrono>
// Constructor for int, float, double, etc...
template<typename T>
Myclass<T>::Myclass …Run Code Online (Sandbox Code Playgroud)