Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++11 chrono unreferenced local variable

Tags:

c++

c++11

When I use the following code to perform some action for 1 second, I get a C4101 warning from Visual Studio: warning C4101: 'highResClock' : unreferenced local variable. 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 looks like the warning from Visual Studio is because std::chrono::high_resolution_clock::now() is a static function. The highResClock variable wasn't needed to access now(), even though that is the particular method I chose to use. Visual Studio seems to interpret this as a non-use of the variable. When I use the following I no longer get 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
}
like image 213
Andy Howe Avatar asked Jun 30 '15 06:06

Andy Howe


People also ask

What is unreferenced local variable?

An unreferenced variable in the source code of a computer program is a variable that is defined but which is never used. This may result in a harmless waste of memory.

What does unreferenced local variable mean in C++?

'value': unreferenced local variable means that you don't use that variable value in any way. In the function shown above, you can see that value is the argument of myfunc but myfunc never actually uses the variable.


1 Answers

You're using a static method on an instance of a class, which causes this warning:

However, this warning will also occur when calling a static member function 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 information about si to access the static function, but the instance of the class is not needed to call the static function; hence the warning [emphasis added].

The MSDN article also provides additional information how to get rid of the warning:

To resolve this warning, you could:

  • Add a constructor, in which the compiler would use the instance of si in the call to func.

  • Remove the static keyword from the definition of func.

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

Since you're using a standard class, you should resort to the latter, e.g. 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
}

That being said, you shouldn't use a busy-loop to wait, there are other ways to do this (e.g. condition variables, or std::this_thread::sleep_*).

like image 171
Zeta Avatar answered Sep 29 '22 14:09

Zeta