I use some code to report duration of a task using std::chrono::high_resolution_clock ... part of c++0x.
I can successfully compile c++0x features in eclipse cdt using the -gnu++0x flag. Although successfully compiling, the editor seemed unaware of c++0x i.e. it displayed errors for any c++0x features in my code. I solved that by adding the -gnu++0x flag to my project discovery options. Note: doesn't show fixed until you do another compile and rebuild the index ...
-E -P -v -dD "${plugin_state_location}/specs.cpp" -std=gnu++0x
I still have one last editor error that I can't rid myself of "Symbol 'duration_cast' could not be resolved" (I had a pic but new users can't post pics)
Anyone have any ideas on how to fix this? Here is the code:
#ifndef _scoped_timer_h_
#define _scoped_timer_h_
#include <iostream>
#include <chrono>
#include "boost/noncopyable.hpp"
#include "boost/format.hpp"
using namespace std::chrono;
// Utility class for timing and logging rates
// (ie "things-per-second").
// NB _any_ destructor invokation (including early return
// from a function or exception throw) will trigger an
// output which will assume that whatever is being measured
// has completed successfully and fully.
class scoped_timer : boost::noncopyable
{
public:
scoped_timer(
const std::string& what,
const std::string& units,
double n
)
:_what(what)
,_units(units)
,_how_many(n)
,_start(high_resolution_clock::now())
{}
~scoped_timer() {
high_resolution_clock::time_point stop = high_resolution_clock::now();
const double t = 1e-9 * duration_cast<nanoseconds>(stop-_start).count();
std::cout << (
boost::format(
"%1%: %|2$-5.3g| %|3$|/s (%|4$-5.3g|s)"
) % _what % (_how_many/t) % _units % t
) << std::endl;
}
private:
const std::string _what;
const std::string _units;
const double _how_many;
const high_resolution_clock::time_point _start;
};
#endif
Eclipse has it's own parser. That parser can't deal with the c++11 (C++0x) features. So you have to wait until the c++11 support in the eclipse parser will be ready.
For chrono in eclipse you should add those symbols
_GLIBCXX_USE_C99_STDINT_TR1
and
__cplusplus = 201103L
How to add them:
Prject properties -> C/C++ General -> Path and Symbols -> Symbols (Tab) -> GNU C++ -> and there click add.
Remember to add __cplusplus
with value 201103L
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With