做一些事情是否有意义,例如放入一个std::lock_guard额外的范围,以便锁定时间尽可能短?
伪代码:
// all used variables beside the lock_guard are created and initialized somewhere else
...// do something
{ // open new scope
std::lock_guard<std::mutex> lock(mut);
shared_var = newValue;
} // close the scope
... // do some other stuff (that might take longer)
Run Code Online (Sandbox Code Playgroud)
除了锁定持续时间短之外还有其他优点吗?
什么可能是负面影响?
我希望在 VS 2019 测试资源管理器中显示用 google test 编写的 c++ 单元测试。
测试设置正确并且可以执行。结果显示在 VS 调试控制台/类似命令行的窗口中。除了测试相关消息之外,不显示任何错误消息。我想从测试资源管理器开始测试并想创建测试播放列表。
我安装了 VS 安装程序提供的 google 测试适配器。我遵循了TestAdapterForGoogleTest中的指南和建议的故障排除。
是否存在另一种方法可以让谷歌测试在测试资源管理器中显示?谷歌测试和 VS 测试资源管理器还有哪些其他已知的不兼容性?
如果模板参数不符合条件(某些值或类型),如何阻止生成模板化类的函数?可以通过使用c ++代码而不使用预处理程序指令来实现吗?
标题和正文中都不应提供这种功能。这种情况似乎有点人为,但我还没有找到解决方案。
示例类(简化-没有构造函数等):
MyClass.h
template<int Dimension, typename TPixelType = float>
class MyClass
{
void DoSomething();
void DoAnotherThing(); // this function should only be available if Dimension > 2
}
Run Code Online (Sandbox Code Playgroud)
MyClass.cxx
template< int Dimension, typename TPixelType> void MyClass::DoSomething()
{...}
// pseudocode: if Dimension <= 2 do not compile next function
template< int Dimension, typename TPixelType> void MyClass::DoAnotherThing()
{
MethodNotToBeCompiled(); // The function I don't want to be executed
}
Run Code Online (Sandbox Code Playgroud)
TestMyClass.cxx
int main()
{
auto myclass0 = MyClass<2>();
myclass0.DoSomething(); // OK …Run Code Online (Sandbox Code Playgroud)