2

std::chrono ought to be supported in g++ 4.8.*. However, when I try to compile using it using g++ 4.8.3, it cannot find various declarations. I am, of course, using -std=c++11.

For example this invocation (from an autogenerated file; that's why the -std appears twice):

g++-4.8 -g -msse2 -m64 <defines> <warnings> -std=c++11 -fexceptions -std=c++11 <includes'-path> -c <source-file.cpp> -o <out-path>

Produces this error:

<source-file, line>: error: ‘std::chrono::monotonic_clock’ has not been declared

I wasn't able to find very much that wasn't immediately a compiler version or missing -std=c++11. By inference from this, I shouldn't need anything else.

Question: what's wrong, how do I fix it?

Community
  • 1
  • 1
geometrian
  • 14,775
  • 10
  • 56
  • 132

2 Answers2

2

There is no std::chrono::monotonic_clock in standard C++. There is a std::chrono::steady_clock, however.

In fairness to Microsoft - and burritos everywhere - there was a monotonic_clock in the working drafts during the development of C++11 which was replaced by steady_clock.

Community
  • 1
  • 1
Casey
  • 41,449
  • 7
  • 95
  • 125
  • Technically, its existence is implementation-dependent. The code, for example, compiles in MSVC. Thanks. – geometrian Nov 24 '14 at 19:41
  • @imallett The standard allows implementations to put additional names in namespace `std` and nested namespaces, so there could well be a `std::chrono::i_like_burritos` in some implementation somewhere. But there is no requirement that namespace `chrono` has a member named `i_like_burritos` (or `monotonic_clock`) in C++11. (Or C++14, which is a shame because I really do like burritos.) – Casey Nov 24 '14 at 19:44
  • For sure. I'm just saying that `std::chrono::monotonic_clock` is a burrito. – geometrian Nov 24 '14 at 19:48
1

It seems to me that you could use code like this to determine whether you're using an old library implementation (that provides monotonic_clock but not steady_clock) or a new one (that provides steady_clock but possibly not monotonic_clock).

#if defined(__GLIBCXX__) && (__GLIBCXX__ < 20120322)
typedef std::chrono::monotonic_clock steady_clock;
#else
typedef std::chrono::steady_clock steady_clock;
#endif

The datestamp above corresponds to the libstdc++ shipped with GCC 4.7.0, according to GNU. I'd welcome any improvements or corrections to this code, for example to support libraries other than libstdc++.

Quuxplusone
  • 23,928
  • 8
  • 94
  • 159