Declare a recursive variable

I just saw this black magic in stupidity /ManualExecutor.h

TimePoint now_ = now_.min(); 

After I processed the entire source code of the library, I did not see the definition of the now_ variable anywhere else except here. What's going on here? Is this actually a declaration of some recursive variable?

+44
variables c ++ 11 recursion folly
Dec 07 '16 at 13:38 on
source share
2 answers

This code is most likely equal to this:

 TimePoint now_ = TimePoint::min(); 

This means that min() is a static method, and calling it using an instance is similar to calling it, this instance is used only to determine the type. There was no black magic, these are just two syntaxes to do the same.

As for the compiled code: now_ already declared by the left side of the line, so when it is used for initialization on the right side, the compiler already knows its type and can call a static method. Attempting to call a non-static method should give an error (see @ BenVoigt comment below).

As the fact that you had to write this question showed that the syntax in the question is not the clearest. It can be tempting if the name is of type long and perhaps justified in declarations of member variables with an initializer (which is the question code). Inside the internal code functions, auto is the best way to reduce repetition.

+62
Dec 07 '16 at 13:45
source share
— -

Digging in the code shows that TimePoint is an alias for chrono :: stable_clock :: time_point, where min () is really a static method that returns the minimum allowed duration:

http://en.cppreference.com/w/cpp/chrono/time_point/min

+14
Dec 07 '16 at 18:31
source share



All Articles