C++ c++;11计时未引用局部变量
当我使用以下代码执行某些操作1秒时,我从VisualStudio收到一条C4101警告:警告C4101:“highResClock”:未引用的局部变量。我不明白为什么在声明后两次使用highResClock时会收到此警告C++ c++;11计时未引用局部变量,c++,c++11,C++,C++11,当我使用以下代码执行某些操作1秒时,我从VisualStudio收到一条C4101警告:警告C4101:“highResClock”:未引用的局部变量。我不明白为什么在声明后两次使用highResClock时会收到此警告 chrono::high_resolution_clock highResClock; chrono::duration<int, ratio<1, 1> > dur(1); chrono::time_point<chrono::high_reso
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
}
chrono::高分辨率时钟高重锁;
时间:持续时间(1);
chrono::time_point end=highResClock.now()+dur;
while(highResClock.now()
编辑:看起来来自Visual Studio的警告是因为std::chrono::high_resolution_clock::now()是一个静态函数。访问now()不需要highResClock变量,即使这是我选择使用的特定方法。VisualStudio似乎将此解释为不使用变量。当我使用以下命令时,我不再收到任何警告:
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
}
chrono::持续时间dur(1);
时钟::时间点结束=时钟::高分辨率时钟::现在()+dur;
while(chrono::高分辨率时钟::now()
您正在类的实例上使用静态方法,该方法:
但是,当通过类的实例调用静态成员函数时,也会出现此警告:
// 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;
}
在这种情况下,编译器使用有关si
的信息来访问静态函数,但是调用静态函数不需要类的实例;因此发出警告[强调添加]
MSDN文章还提供了如何消除警告的其他信息:
要解决此警告,您可以:
- 添加一个构造函数,其中编译器将在调用
时使用func
的实例si
- 从
的定义中删除func
关键字static
- 显式调用静态函数:
int y=S::func()代码>
std::chrono::high_resolution\u clock::now()
:
auto end=std::chrono::high_resolution_clock::now()+std::chrono::seconds(1);
while(std::chrono::高分辨率时钟::now()
话虽如此,您不应该使用忙循环等待,还有其他方法(例如,条件变量或
std::this\u thread::sleep.
)。备注:std::chrono::high\u resolution\u clock::now
是一个静态函数,可以在没有std::chrono::high\u resolution\u clock
实例的情况下使用。我不知道为什么VS会给你C4101,但那可能是source.FWIW,编译无警告。同样适用于
auto end = std::chrono::high_resolution_clock::now() + std::chrono::seconds(1);
while(std::chrono::high_resolution_clock::now() < end)
{
// do nothing
}