3

In the example here: https://en.cppreference.com/w/cpp/chrono/high_resolution_clock/now

They declared the clock time point with auto.

auto start = std::chrono::high_resolution_clock::now();

and the doc says it returns 'A time point representing the current time.'

But I am not sure how do declare in my code below because I am used to declaring the variables at the beginning of the function and I don't know what to declare it as. Code has been simplified here to show what I mean. What do I put for the ????

I already tried auto there but the compiler won't allow it. auto orderRecvedTime; gives me this error:

error: non-static data member declared with placeholder 'auto'
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <string.h>
//#include "load_symbol.h"
//#include "check_symbol.h"
#include "windows.h"
#include <vector>
#include <chrono>
using namespace std;

 

class order {
  private:
    string orderID;
    ???    orderRecvedTime;
    char   buysell;
    string symbol;
    double price;
    int    qty;

  public:
    void newOrder(string &_orderID, char &_buysell, string &_symbol, double &_price, int &_qty){
        orderID = _orderID;
        buysell = _buysell;
        symbol = _symbol;
        price = _price;
        qty = _qty;
        orderRecvedTime = std::chrono::high_resolution_clock::now();
    }
  
};



int main() {
    cout << "!!!Hello once more" << endl; // prints !!!Hello once more

    vector<order> thebook;
    string user_order = "";

    string done = "done trading";
    string orderID;
    string orderaction;
    string orderRecvedTime;
    char buysell;
    string symbol;
    double price;
    int qty;

    while (user_order.compare(done) != 0) {
        cout << "enter order"<< endl;
        getline(cin, user_order);

        stringstream lineStream(user_order);
        lineStream >>orderaction>>orderID>> buysell >> symbol >> price>> qty;
 
        order user_order;
        if (orderaction.compare("D") == 0) {
            cout << "you are making a new order."<< endl;
            user_order.newOrder(orderID, buysell,symbol,price,qty);
            thebook.push_back(user_order);
        }
    }
}
Dr. Gut
  • 2,053
  • 7
  • 26
D.Zou
  • 761
  • 8
  • 25
  • 2
    Google for high_resolution_clock::now, and you'll see a description with type (the word `static` is not part of the type). – user31264 Jun 04 '20 at 02:40
  • 1
    you mean this: std::chrono::time_point? – D.Zou Jun 04 '20 at 02:41
  • I tried to use the ```std::chrono::time_point orderRecvedTime``` but got an error from compiler: ``` error: invalid use of template-name 'std::chrono::time_point' without an argument list ``` – D.Zou Jun 04 '20 at 02:49
  • 2
    @D.Zou The page you linked has the declaration of `now()` at the very top. Everything between `static` and `now()` is the type of the return value: `std::chrono::time_point`. – dxiv Jun 04 '20 at 02:58
  • 1
    `auto` is often useful. – Eljay Jun 04 '20 at 03:06
  • @dxiv what the difference between these two? ``` std::chrono::_V2::system_clock::time_point orderRecvedTime; this compiles std::chrono::time_point orderRecvedTime; ``` they both compiled, but first is pulled from my debug error msg when I tried double long and the 2nd is from the webpage. – D.Zou Jun 04 '20 at 03:06
  • @D.Zou You should use the standard definition. What you copied from the debugger is an implementation internal, not portable and not to be relied upon. – dxiv Jun 04 '20 at 03:09
  • The `std::chrono::time_point` solution will determine the type based on what type of clock is used, so you could also use it where `T` is `system_clock` or `steady_clock`. This makes this solution a lot more robust than the other method. – bisen2 Jun 04 '20 at 03:10

2 Answers2

3
std::chrono::high_resolution_clock::time_point orderRecvedTime;

In practice, high_resolution_clock is a type alias for either system_clock or steady_clock, so my advice is to choose either one of those instead for a portable experience.

  • system_clock is like a watch. It can tell you what time it is.
  • steady_clock is like a stop watch. It is really good for timing things, but doesn't really know the time of day.
Dr. Gut
  • 2,053
  • 7
  • 26
Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
-1

pulled this from the debugger msg but if i declare it like this it compiles.

std::chrono::_V2::system_clock::time_point orderRecvedTime;

D.Zou
  • 761
  • 8
  • 25