Exploration of macro and inline functions-Error Reflection caused by custom min Functions

Source: Internet
Author: User

In C ++ programming, functions (including inline functions) are generally in lower case, while macro-defined "functions" (macros with parameters) are often in upper case.

The above sentence seems common, but failing to follow this sentence can easily lead to unexpected errors! Today, we will record a typical case:

Because inline functions are very similar to macros, they are performed before the program runs. They both replace expressions with the function body and avoid the overhead caused by function calls to improve efficiency, therefore, it is easy to blur the essential differences between the two, so that you forget the words at the beginning of this article. No, I did it today. This does not conform to the programming specifications, but it does not necessarily lead to errors. Unless the inline function name and the macro with parameters have the same name, if no error is reported for the function parameters, this will lead to difficult troubleshooting errors! Next, I will detail this instance.

1. The order of macros and inline functions

At the beginning, I customized a Class A. it was okay to test a. Later, when I expanded Class A, its member functions needed to call another class B. The program did not report an error, however, the calculation result is obviously incorrect. After debugging, the problem is finally locked: Class A contains class B header files, and Class B header files contain two header files related to opencv, the CV namespace is also used:

#include "opencv2/imgproc/imgproc.hpp"#include "opencv2/highgui/highgui.hpp"using namespace cv;

It's really strange. I guess it's caused by namespace conflicts? No. The program does not report an error. Therefore, it is only possible that the function or variable name in the two header files conflicts with the function or variable name in Class A. However, if the name is the same, it must be a priority to call my custom functions! The only possible cause of the error is that a function or variable name is used in the member function of Class A and the function or variable name in opencv conflict before running, that is, the name conflicts during pre-compilation or compilation.

After carefully checking the code, I found that I customized an inline function in Class:

inline float MIN(float a, float b, float c, float d){float t1,t2;t1 = a<b?a:b;t2 = c<d?c:d;return t1<t2?t1:t2;}

The imgproc. HPP and highgui. HPP contained in Class B have the following statements:

#include "opencv2/core/core.hpp"#include "opencv2/imgproc/types_c.h"
In types_c.h, I found:

#ifndef MIN#  define MIN(a,b)  ((a) > (b) ? (b) : (a))#endif
Therefore, the cause of the error is that opencv macro defines min, and the program calls the opencv macro before running to replace the expression, instead of using a custom inline function.

Why? In fact, the macro definition is implemented during pre-compilation, while the inline function is implemented during compilation. When the name is duplicated, the macro definition is replaced!

Of course, if macro is used to customize min at the beginning, this error will not be caused. In short, it is a problem caused by the use of uppercase inline functions.

2. Number of macro parameters with Parameters

Here is another question: Since we replaced my expression with opencv min during pre-compilation, I passed four parameters instead of two parameters in the macro definition. Why can we still calculate the result? The vs2005 compiler does. However, if only one parameter is passed, an error is returned. An experiment proves:

#define MINTEST(a,b) (a<b?a:b)void main(){float a = MINTEST(12,9,8);cout<<a<<endl;}
Output result: 9

#define MINTEST(a,b) (a<b?a:b)void main(){float a = MINTEST(12);cout<<a<<endl;}

Compilation error prompt: Error c2059: syntax error :'? '
It can be seen that during macro replacement, the pre-compiler extracts parameters in sequence to replace them. If the number of parameters exceeds the limit, the following parameters are ignored. If the number of parameters is insufficient, the expression after replacement is incorrect, therefore, compilation errors are caused.

3. Summary

1. Follow the naming rules. It makes sense not to use uppercase letters for inline functions.

2. The number of macro-defined parameters with parameters can be exceeded, but cannot be insufficient. Otherwise, compilation errors may occur.



Exploration of macro and inline functions-Error Reflection caused by custom min Functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.