c++autodecltypenon-staticmember-variables

how to declare a var of the type of a member var of some struct?


I want to get codes like this:

struct Order_t {
   time_point<system_clock, microseconds>  order_time;
   // some other fileds
};

template<typename Dura>
void onTimer( time_point<system_clock, Dura> tp_now ) {
    auto tp0 =  time_point_cast<Order_t::order_time::duration>( tp_now );

    // some other codes...
};

But these can not be compiled. In fact I need to declare a variable that has same type as Order_t::order_time, but there is no var of the type here.


Solution

  • To get a nested type (::duration) you need a type, not a variable. Hence, it should be

    auto tp0 = time_point_cast<decltype(Order_t::order_time)::duration>(tp_now);