该A()宏只会在 MSVC 上扩展,而不会在 GCC/Clang 上扩展,除非A()带有前缀,例如Test A().
通过在-E( /E) 标志 ( Godbolt.org )下运行以下代码段:
#define A() HelloWorld::
#define B() ::
A()
B()
Run Code Online (Sandbox Code Playgroud)
我们看到 MSVC 给出了以下输出:
HelloWorld::
::
Run Code Online (Sandbox Code Playgroud)
而 GCC/Clang 给出了不同的输出:
::
Run Code Online (Sandbox Code Playgroud)
但是然后运行这个片段:
#define A() HelloWorld::
A()
Test A()
Run Code Online (Sandbox Code Playgroud)
在所有 3 个编译器上为我们提供以下信息:
HelloWorld::
Test HelloWorld::
Run Code Online (Sandbox Code Playgroud)
为什么 GCC/Clang 输出缺少第一行?为什么它在Test A()写入时正确扩展所有事件?这是在标准中明确定义的,还是特定于编译器的?
我有一个相当普遍的问题:我最近在 C/C++ 代码中看到许多#defines 被注释为“/*< ... */”,例如:
#define ARRSIZE(x) (sizeof(x)/sizeof(x[0])) /*< macro to determine the size of an array */
Run Code Online (Sandbox Code Playgroud)
我个人只在定义的评论中看到这一点,谷歌搜索也没有回答我的问题。那有什么意义吗?这只是常见的做法还是来自 Doxygen?还是有其他原因?
我有一个 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) 我正在使用称为Secugen Pro 20的设备捕获指纹,它拥有自己的Linux SDK,并且我想捕获指纹图像并将其保存为任何图像格式。
他们有这个 typedef unsigned char BYTE;
我声明了我的imageBuffer
BYTE *CurrentImageBuffer;
然后我使用设备规格为其分配内存
CurrentImageBuffer = malloc(device_info.ImageWidth*device_info.ImageHeight);
在我的代码的某个点上,我捕获图像并将其CurrentImageBuffer作为参数传递给捕获函数:
SGFPM_GetImageEx(m_hFPM, CurrentImageBuffer, GET_IMAGE_TIMEOUT, NULL, GET_IMAGE_DESIRED_QUALITY)
那就是该行代码之后变量的外观(我可以确认它抓住了一根手指):

我只是不明白如何从该缓冲区继续创建图像,因为它看起来不像ByteArray
我什至不知道那是从中获取我的图像的正确地方,但是那看起来像正确的地方,因为它有缓冲区,对吧?
OBS:我是C新手
有人能想出一个干净(和快速)的解决方案来解决以下问题:
struct Value {
int index = 0;
int cost = 0;
}
Run Code Online (Sandbox Code Playgroud)
index应该只包含在序列中一次,并且cost应该累积每个重复索引。我想出的基本解决方案对序列进行排序,当在BinaryPredicate传递给中检测到相等的条目时std::sort,cost将相加到lhs. 然后将的成本rhs设置为 0。然后跟随一个remove_if删除 0 成本值。请参见此处的示例:
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <iostream>
struct Value
{
int index = 0;
int cost = 0;
};
// generate a bunch of random values in a vector
// values will have indices in range [0..10]
std::vector<Value> generator()
{
std::vector<Value> …Run Code Online (Sandbox Code Playgroud) 假设我声明了这样的函数noexcept:
int pow(int base, int exp) noexcept
{
return (exp == 0 ? 1 : base * pow(base, exp - 1));
}
Run Code Online (Sandbox Code Playgroud)
从我对 C++ 的了解很少,但慢慢增长的知识来看,noexcept当我确定该函数不会抛出异常时,我可以做到这一点。我还了解到它可以在某个值范围内,假设我在小于 10 和小于 8noexcept时考虑我的函数(仅作为示例)。我如何声明这个函数处于这样的值范围内?或者我最多能做的就是给其他程序员留下评论,说它应该在某个特定的范围内?expbasenoexcept
我正在使用 win32 API,并使用它来制作一个窗口。该窗口可以工作,但是当我打开它时,光标是加载光标,每次我将光标移至边缘以调整其大小时,光标都会“卡住”,因为调整光标大小时,它不会恢复正常。这是一个视频来解释我在说什么:
这是可重现的示例(使用 编译g++ reproducible_example.cpp -mwindows -O3 -o reproducible_example.exe):
#undef UNICODE
#undef _UNICODE
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
bool isRunning = true;
void *buffer; // buffer memory
BITMAPINFO bmi; // bit map information, needed for rendering
int width, height; // main window's width and height
LRESULT __stdcall WindowProc(HWND, UINT, WPARAM, LPARAM);
int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR pCmdLine, int nCmdShow) {
LPCSTR CLASS_NAME = "Class Name";
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName …Run Code Online (Sandbox Code Playgroud) 在《C++ 编程语言》第 17.5.1.4 节中,有以下示例:
\nstruct Base {\n int b;\n Base(const Base&);\n};\n\nstruct Derived {\n int d;\n Derived(const Derived&);\n};\n\nvoid naive(Base* p) {\n B b2 = *p;\n};\n\nvoid user() {\n Derived d;\n naive(&d);\n Base bb = d;\n};\nRun Code Online (Sandbox Code Playgroud)\nI\xe2\x80\x99 省略了代码中的注释,并在naive() (应该是Base b2 ?) 中包含了明显的拼写错误。
我\xe2\x80\x99m 只是混淆了问题到底是什么:我没有理由期待一个Derived 在任何一种情况下都会复制 a 。我觉得好像我应该在这里遗漏一些东西,但我只是无法理解这个 \xe2\x80\x9cexample\xe2\x80\x9d 的意义。
我有下面的代码,我认为基于Find Memory Leaks Using the CRT Library,应该打印出内存泄漏的行号。
#include "stdafx.h"
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include <iostream>
void derp()
{
int* q = new int;
}
int main()
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
derp();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我得到以下信息:
Detected memory leaks!
Dumping objects ->
{75} normal block at 0x0067E930, 4 bytes long.
Data: < > CD CD CD CD
Object dump complete.
Run Code Online (Sandbox Code Playgroud)
根据 Microsoft 的文档,我希望看到分配泄漏内存的行的打印输出,但我没有。
我做错了什么?我正在使用 VS2015。
在将遗留代码移植到 C++20 时,我将字符串文字(带有预期的 UTF-8 编码文本)替换为 UTF-8 字符串文字(前缀为u8)。
因此,我遇到了八进制序列的问题,我过去用它来逐字节编码 UTF-8 序列:
\n虽然
\n"\\303\\274"是 的正确编码\xc3\xbc,但
\nu8"\\303\\274"最终以\xc3\x83\xc2\xbc.
我对此进行了进一步调查,并在cppreference.com上发现:
\n\n\n\n
(强调我的)
\n用我自己的话说:在 UTF-8 字符串文字中,八进制 ( \\ooo) 和十六进制 ( \\xXX) 转义序列被解释为 Unicode 代码点,类似于 Unicode 序列 (\\uXXXX和\\UXXXXXXXX)。 …
c++ ×9
c ×2
algorithm ×1
c++20 ×1
clang ×1
comments ×1
console ×1
crt ×1
doxygen ×1
fingerprint ×1
gcc ×1
image ×1
interaction ×1
linux ×1
memory-leaks ×1
noexcept ×1
output ×1
precompile ×1
stl ×1
utf-8 ×1
visual-c++ ×1
winapi ×1
window ×1
windows ×1