【问题标题】:In a template is there a way to write only one specialization for every chrono instantiation? (nanoseconds, milliseconds, seconds, etc)在模板中,有没有办法为每个计时实例只编写一个专业化? (纳秒、毫秒、秒等)
【发布时间】:2018-04-08 10:27:59
【问题描述】:

我有一个模板需要使用以下类型:intfloatdoublestd::chrono::nanosecondsstd::chrono::millisecondsstd::chrono::seconds

该模板有一个成员函数可与intfloatdouble 一起使用,但我需要编写一个专门针对纳秒、另一个针对毫秒和另一个针对秒。

为了得到一个工作代码,我不得不为每个std::chrono“类型”编写一个专业化的代码,代码基本相同。所以我失去了使用模板的优势。

我已经读到 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 ( ) :
    // Here we are setting m_max to the maximum value
    m_max( std::numeric_limits<T>::max( ) )
{}

// Constructor: Specialization number 1 for std::chrono::nanoseconds
template<> 
Myclass<std::chrono::nanoseconds>::Myclass()  :
    // Here we are setting m_max to the maximum value
    m_max( std::chrono::nanoseconds::max( ) )
{}

// Constructor: Specialization number 2 for std::chrono::milliseconds
template<> 
Myclass<std::chrono::milliseconds>::Myclass ( ) :
    // Here we are setting m_max to the maximum value
    m_max( std::chrono::milliseconds::max() )
{}

// Constructor: Specialization number 3 for std::chrono::seconds
template<> 
Myclass<std::chrono::seconds>::Myclass ( ) :
    // Here we are setting m_max to the maximum value
    m_max( std::chrono::seconds::max( ) )
{}

// Forcing instantiations
template class Myclass<int>;
template class Myclass<float>;
template class Myclass<double>;

template class Myclass<std::chrono::nanoseconds>;
template class Myclass<std::chrono::milliseconds>;
template class Myclass<std::chrono::seconds>;

// file: main.cc

#include <iostream>

int main() {
    Myclass<int>                       intobj;
    Myclass<float>                     floatobj;
    Myclass<double>                    doubleobj;
    Myclass<std::chrono::nanoseconds>  nanosecondseobj;
    Myclass<std::chrono::milliseconds> millisecondseobj;
    Myclass<std::chrono::seconds>      secondseobj;

    using std::cout;
    using std::endl;
    cout << "Maximum value for int = "          << intobj.m_max << endl;
    cout << "Maximum value for float = "        << floatobj.m_max << endl;
    cout << "Maximum value for double = "       << doubleobj.m_max << endl;
    cout << "Maximum value for nanoseconds = "  << nanosecondseobj.m_max.count( ) << endl;
    cout << "Maximum value for milliseconds = " << millisecondseobj.m_max.count( ) << endl;
    cout << "Maximum value for seconds = "      << secondseobj.m_max.count( ) << endl;
    return 0;
}

【问题讨论】:

  • "我有一个模板需要使用以下类型:" 为什么?为什么你的模板可以与nanosecondsmilliseconds 一起使用,但不能与microseconds 一起使用?这会造成一个可怕的界面。事实上,duration 接口的重点 是允许两个不同用户使用的单元之间的互操作。如果你提供一个需要时间的函数,如果我通过microsecondsnanoseconds,它应该能够正常工作。
  • 另外,问题中没有答案;他们进入答案部分。
  • @NicolBolas 如果您仔细阅读该问题,您会注意到问题是:“在模板中是否有一种方法可以只为 每个 chrono 实例化编写一个特化?(纳秒、毫秒、秒等)”我发布的示例代码仅用于澄清我的问题和学习目的。
  • @NicolBolas 将“完整使用演示”移至提交部分

标签: c++ c++11 templates chrono specialization


【解决方案1】:

你可以对std::chrono::duration进行部分特化

template<typename T, typename Ratio> 
class Myclass<std::chrono::duration<T,Ratio> > {
public:
  // Variable (I want to initialize m_max to the maximum value possible for the 
  // T type)
  std::chrono::duration<T,Ratio> m_max ;

  // Constructor
  Myclass ( ) : m_max(std::chrono::duration<T,Ratio>::max()) {}
};

使用演示: https://wandbox.org/permlink/Q8R5pz2UPawnZ1NG

【讨论】:

  • 非常感谢您的快速答复!我将复制您的使用演示示例以供将来阅读。
【解决方案2】:

完整的使用示范发布者:chtz

#include <iostream>
#include <limits>
#include <chrono>

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 ( ) : m_max(std::numeric_limits<T>::max()) {}
};

template<typename T, typename Ratio> 
class Myclass<std::chrono::duration<T,Ratio> > {
public:
  // Variable (I want to initialize m_max to the maximum value possible for the 
  // T type)
  std::chrono::duration<T,Ratio> m_max ;

  // Constructor
  Myclass ( ) : m_max(std::chrono::duration<T,Ratio>::max()) {}
};

int main() {
    Myclass<int>                       intobj;
    Myclass<float>                     floatobj;
    Myclass<double>                    doubleobj;
    Myclass<std::chrono::nanoseconds>  nanosecondseobj;
    Myclass<std::chrono::milliseconds> millisecondseobj;
    Myclass<std::chrono::seconds>      secondseobj;

    using namespace std;
    cout << "Maximum value for int = "          << intobj.m_max << endl;
    cout << "Maximum value for float = "        << floatobj.m_max << endl;
    cout << "Maximum value for double = "       << doubleobj.m_max << endl;
    cout << "Maximum value for nanoseconds = "  << nanosecondseobj.m_max.count( ) << endl;
    cout << "Maximum value for milliseconds = " << millisecondseobj.m_max.count( ) << endl;
    cout << "Maximum value for seconds = "      << secondseobj.m_max.count( ) << endl;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-17
    • 1970-01-01
    • 1970-01-01
    • 2015-11-14
    相关资源
    最近更新 更多