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
}
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.
'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.
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 tofunc
.Remove the
static
keyword from the definition offunc
.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_*
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With