我有一个 C++ 程序(MSVC 2017),它通过 std::cout 不断输出调试信息。然而,有时当我与控制台进行物理交互时(例如意外点击它),它会停止产生输出。这意味着没有打印任何内容,尽管程序继续运行并完成正确执行的任何操作。
任何想法如何解决这一问题?使用“std::cout.setf(std::ios::unitbuf);”删除 std::cout 缓冲区 没有效果。
样本:
#include <iostream>
int main()
{
int i = 0;
while (true) {
i++;
if (i%100000000 == 0) std::cout << i++ << "\n";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 函数执行两次代码块:对于cnt = 0和cnt = 1.目前我使用以下实现:
int func {
int V1, V2, ... , Vn;
#define cnt 0
// a block of code: some operations with V1, ... , Vn
#undef cnt
#define cnt 1
// the same block of code
#undef cnt
}
Run Code Online (Sandbox Code Playgroud)
这段代码非常难看.使用内联函数会导致更加丑陋的代码:我需要通过引用将所有涉及的变量传递给函数.因此,我想创建一些封闭.
我不能用类似的东西
struct Nested {
__forceinline void block(const int cnt) {
// block of code
};
};
Run Code Online (Sandbox Code Playgroud)
因为性能原因,不应将V1,...,Vn设为静态.
我尝试使用lambda函数,但即使内联设置为"Any Suitable(/ Ob2)"(即使使用PGO),Visual C++ 2013也无法内联它,这也会损害性能.
有什么建议?