C ++ 11 chrono unprivileged local variable

When I use the following code to perform an action within 1 second, I get Visual Studio warning C4101: warning C4101: "highResClock": local variable without links. I don't understand why I get this warning when I use highResClock twice after declaring it.

chrono::high_resolution_clock highResClock; chrono::duration<int, ratio<1, 1> > dur(1); chrono::time_point<chrono::high_resolution_clock> end = highResClock.now() + dur; while (highResClock.now() < end) { // do something repeatedly for 1 second } 

Edit: It appears that the warning from Visual Studio is due to the fact that std :: chrono :: high_resolution_clock :: now () is a static function. The highResClock variable is not needed for now () access, although this is a special method that I decided to use. Visual Studio seems to interpret this as not using a variable. When I use the following, I no longer receive any warnings:

 chrono::duration<int, ratio<1, 1> > dur(1); chrono::time_point<chrono::high_resolution_clock> end = chrono::high_resolution_clock::now() + dur; while (chrono::high_resolution_clock::now() < end) { // do nothing } 
+5
source share
1 answer

You are using a static method for an instance of a class that raises this warning :

However, this warning also occurs when a static member function is called through an instance of the class:

 // C4101b.cpp // compile with: /W3 struct S { static int func() { return 1; } }; int main() { S si; // C4101, si is never used int y = si.func(); return y; } 

In this situation, the compiler uses si information to access the static function , but the class instance is not needed to call the static function; therefore warning [highlighted by cursor].

The MSDN article also contains additional information on how to get rid of a warning:

To resolve this warning, you can:

  • Add a constructor in which the compiler will use the si instance in the func call.

  • Remove the static from the func definition.

  • Call the static function explicitly: int y = S::func(); .

Since you are using a standard class, you should resort to the latter, for example. std::chrono::high_resolution_clock::now() :

 auto end = std::chrono::high_resolution_clock::now() + std::chrono::seconds(1); while(std::chrono::high_resolution_clock::now() < end) { // do nothing } 

However, you should not use a busy loop to wait, there are other ways to do this (for example, condition variables or std::this_thread::sleep_* ).

+7
source

All Articles