Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a template is there a way to write only one specialization for every chrono instantiation? (nanoseconds, milliseconds, seconds, etc)

I have a template that needs to work with the next types: int, float, double, std::chrono::nanoseconds, std::chrono::milliseconds and std::chrono::seconds.

The template has a member function to work with int, float and double but I need to write one specialization for nanoseconds, another for milliseconds and another for seconds.

To get a working code I'm forced to write one specialization for every std::chrono "type" with basically the same code. So I'm loosing the advantages of using templates.

I have read that the chrono "types" are typedefs of an instantiation of duration.

Is there a way to write only one specialization for nanoseconds, milliseconds and seconds?

Here is how my code looks like:

// 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;
}
like image 916
ClK Avatar asked Apr 08 '18 10:04

ClK


1 Answers

You can make a partial specialization for 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()) {}
};

Usage demonstration: https://wandbox.org/permlink/Q8R5pz2UPawnZ1NG

like image 144
chtz Avatar answered Sep 30 '22 14:09

chtz