包含点的C / C ++宏参数(成员访问运算符)

时间:2019-10-11 17:32:40

标签: c++ c arguments c-preprocessor member-access

如何在包含点(成员访问运算符)的C / C ++宏参数中使用?

示例:

#define M(obj,y) obj.y##x
struct S { struct {int x;} c; int x; };
S s;
s.c.x = 1;
s.x = 2;
M(s,)   // works, 2 (resolves to s.x)
M(s,c.) // error: pasting formed '.x', an invalid preprocessing token

如何使M(s,c.)解析为s.c.x

谢谢您的帮助!

1 个答案:

答案 0 :(得分:3)

令牌粘贴运算符ocl::setUseOpenCL(true); cv::ocl::haveOpenCL(); VideoCapture cap; cap.open(0); UMat frame; UMat gray; for (;;) { cap >> frame; cvtColor(frame, frame, COLOR_RGB2BGR); cvtColor(frame, gray, COLOR_RGB2GRAY); medianBlur(gray, gray, 5); vector<Vec3f> circles; HoughCircles(gray, circles, HOUGH_GRADIENT, 1, gray.rows / 8, // change this value to detect circles with different distances to each other 100, 20, 25, 50 // change the last two parameters // (min_radius & max_radius) to detect larger circles ); for (size_t i = 0; i < circles.size(); i++) { Vec3i c = circles[i]; Point center = Point(c[0], c[1]); // circle center circle(frame, center, 1, Scalar(0, 100, 100), 3, LINE_AA); // circle outline int radius = c[2]; circle(frame, center, radius, Scalar(0, 0, 255), 3, LINE_AA); } if (frame.empty()) break; // end of video stream imshow("this is you, smile! :)", frame); if (waitKey(10) == 27) break; // stop capturing by pressing ESC } 要求其两个操作数是有效的预处理令牌,并产生一个预处理令牌。它通常用于将两个标识符连接为一个标识符。

您要在此处执行的操作不是令牌粘贴。相反,您试图创建类似##s.x的表达式,其中s.c.x部分始终是单个标记。因此,不应使用x运算符。相反,您可以执行以下操作:

##

当您尝试使用#define M(obj, y) obj.y x 运算符时,预处理器会尝试将参数##中的最后一个标记与标记y组合在一起。在x中,c.是令牌,因此结果为.,这不是有效令牌。相反,.x仅作为两个令牌的序列有效。