Hello List!

The following code snippet fails to compile:

   auto us = std::chrono::microseconds(7);
   std::cout << us << std::endl;   // error

(But "std::cout << us.count() << std::endl;" works as expected.)

So it looks like std::chrono::duration isn't set up for "<<".

Stroustrup's c++11 faq:

   http://www.stroustrup.com/C++11FAQ.html

suggests that something like this should work:

   nanoseconds d = monotonic_clock::now() - t;  // we want the result
in nanoseconds
   cout << "something took " << d << "nanoseconds\n";

Should I be able to insert a std::chrono::duration into an ostream?

Here's a test program:

   #include <chrono>
   #include <iostream>
   int main (int argc, char *argv[]) {
     auto us = std::chrono::microseconds(7);
     // std::cout << us << std::endl;  //error
     std::cout << us.count() << std::endl;
   }

If I uncomment the line labelled "error", I get the following
compile-time error:

g++ -std=gnu++11 -o duration_io_test duration_io_test.cpp
duration_io_test.cpp: In function 'int main(int, char**)':
duration_io_test.cpp:5:16: error: cannot bind 'std::ostream {aka
std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'
   std::cout << us << std::endl;  //error
                ^
In file included from .\mingw64\include\c++\4.8.1\iostream:39:0,
                 from duration_io_test.cpp:2:
.\mingw64\include\c++\4.8.1\ostream:602:5: error:   initializing
argument 1 of 'std::basic_ostream<_CharT, _Traits>&
std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&)
[with _CharT = char; _Traits = std::char_traits<char>; _Tp =
std::chrono::duration<long long int, std::ratio<1ll, 1000000ll> >]'
     operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
     ^

I am using the following version of g++:

   C:>g++ --version
   g++ (rubenvb-4.8-stdthread) 4.8.1 20130324 (prerelease)

There is nothing urgent about this for me -- using duration.count() works just
fine for my purposes.  I'm just wondering how things are supposed to work.


Thanks.


K. Frank

------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to