小编ClK*_*ClK的帖子

在模板中是否有一种方法只为每个计时实例化编写一个特化?(纳秒,毫秒,秒等)

我有一个需要下一个类型的工作模板:int,float,double,std::chrono::nanoseconds,std::chrono::millisecondsstd::chrono::seconds.

该模板有一个成员函数一起工作int,floatdouble但我需要编写纳秒,另一个为毫秒,另一个用于秒一个专业化.

为了得到一个有效的代码,我不得不为每个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)

c++ templates specialization c++11 c++-chrono

10
推荐指数
1
解决办法
314
查看次数

标签 统计

c++ ×1

c++-chrono ×1

c++11 ×1

specialization ×1

templates ×1