Search code examples
c++castingc++-chrono

Casting time_points: dependent name error


dependent-name 'BaseClock:: time_point' is parsed as a non-type, but instantiation yields a type

is the error I get for the following code:

struct H { SomeClock::time_point time; };
template<typename BaseClock>
void RelativeClock<BaseClock>::someMethod(H h) {
    //...
    typename BaseClock::time_point newtime;
    newtime = time_point_cast<BaseClock::time_point>(h.time);
    //...
}

The SomeClock::time_point can be the SystemClock::time_point for this example, while it is actually some time_point in Microseconds granularity. The important bit is, that it can be different type than the BaseClock::time_point, but also the same, which it usually is. I attempted the cast in the last line to alleviate the problems creeping up when the types of BaseClock and SystemClock differ, which it usually doesn't in my case. It broke one of my tests, though, where I actually use different Clock-Types. What's even more weird is that the types the template parameters actually get resolved to should both be the same, as in implementation they are both based on std::chrono::microseconds. I am, currently, hesitant to just specificly implement the method for the used type in the test and am looking for a generic option.

How do I make the assignment generic in more ways than just the class' template? How do I circumvent the error given at the top?


Solution

  • This should fix your compilation error:

    newtime = time_point_cast<typename BaseClock::time_point>(h.time);
    

    You were missing the typename there. It's needed to indicate that time_point needs to be a type and not a value.