bzm3r
bzm3r

Reputation: 4596

How do I print a `chrono` duration with units with Howard Hinnant's date.h?

I have the following toy code:

#include "date/date.h"
#include <iostream>

using namespace std;
using namespace chrono;

int main() {
    cout << microseconds(100);
};

But, it doesn't work because:

C2679: binary '<<': no operator found which takes a right-hand operand of type 'std::chrono::milliseconds' (or there is no acceptable conversion)

But the docs for date.h list the specification:

template <class CharT, class Traits, class Rep, class Period>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os,
           const std::chrono::duration<Rep, Period>& d);

which will use get_units to output appropriate units.

So, how can I properly use this overloaded << operator?

Upvotes: 3

Views: 306

Answers (1)

interjay
interjay

Reputation: 110069

That operator<< is inside the date namespace. Since neither of the operand types are from this namespace, argument-dependent lookup won't find it.

To use it you need either using namespace date or using date::operator<<.

Another issue in your code is that microseconds can only be constructed from an integer type, not floating point.

Upvotes: 2

Related Questions