2013/7/5 K. Frank <[email protected]>

> 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.
>

Both GCC 4.8 with libstdc++:
http://coliru.stacked-crooked.com/view?id=71573ce00efe8788aa6fcd371fd4e2c0-92a6b8b905b3338bdfc1eb08c231d068
and clang 3.3 with libc++:
http://coliru.stacked-crooked.com/view?id=71573ce00efe8788aa6fcd371fd4e2c0-5850791944d571e696c4e044384770e7
show what you are doing isn't right. I don't have the willpower to dig up a
Standard quote, but this page might give you some hints on how to output
<chrono> durations:
http://en.cppreference.com/w/cpp/chrono/duration

Keep that website bookmarked, it's an awesome reference.

Ruben


>
>
> 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
>
------------------------------------------------------------------------------
See everything from the browser to the database with AppDynamics
Get end-to-end visibility with application monitoring from AppDynamics
Isolate bottlenecks and diagnose root cause in seconds.
Start your free trial of AppDynamics Pro today!
http://pubads.g.doubleclick.net/gampad/clk?id=48808831&iu=/4140/ostg.clktrk
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to