lundi 22 mai 2017

Boolean expression as template argument

I'm trying to set the resolution of a timer class using a bit of template metaprogramming. Here's a bit of a problem which has got me a little stumped. The boolean expression passed as argument is being disregarded.

// Boolean SFINAE struct for partial specialization selection.
template< bool T > struct SFINAE_void;
template<> struct SFINAE_void< true >  { using type = void; };
template<> struct SFINAE_void< false > {};

/* Precision selector */
template< std::intmax_t , std::intmax_t , typename=void > 
struct ChosenResolution;

template< std::intmax_t NUM, std::intmax_t DEN > 
struct ChosenResolution< NUM, DEN, 
typename SFINAE_void< 
( real_cast(NUM)/real_cast(DEN) <  real_cast(1.0) && 
  real_cast(NUM)/real_cast(DEN) >= real_cast(10^-3) ) >::type > {
   using type = std::chrono::milliseconds;
};

template< std::intmax_t NUM, std::intmax_t DEN > 
struct ChosenResolution< NUM, DEN,
typename SFINAE_void< 
( real_cast(NUM)/real_cast(DEN) <  real_cast(10^-3) && 
  real_cast(NUM)/real_cast(DEN) >= real_cast(10^-6) ) >::type > {
   using type = std::chrono::microseconds;
};

template< std::intmax_t NUM, std::intmax_t DEN > 
struct ChosenResolution< NUM, DEN, 
typename SFINAE_void< 
( real_cast(NUM)/real_cast(DEN) <  real_cast(10^-6) && 
  real_cast(NUM)/real_cast(DEN) >= real_cast(10^-9) ) >::type > {
   using type = std::chrono::nanoseconds;
};

Debug output shows me that the resolution is 10^-9. However, std::chrono::milliseconds is being chosen as the resolution, instead of std::chrono::nanoseconds.

Two questions:

1.) Why?

2.) Slightly divergent - but can a normal, everyday laptop really boast of nanosecond resolution (I am using a steady clock)?

Aucun commentaire:

Enregistrer un commentaire