开发者

What's the meaning of #define when there is only 2 part?

I know the following 3 parts #define:

#define PI 3.4

which mean it will replace PI with 3.4.

But that's the meaning o开发者_开发知识库f 2 parts #define like this:

#define something

Will it replace something with null/empty string?

The following is the code example, I searched the file, only list the related lines

  D:\mariadb\storage\pbxt\src\cache_xt.cc (23 hits)  
    Line 172: #ifdef xtPublic  
    Line 173: #undef xtPublic  
    Line 188: #define xtPublic  
    Line 325: xtPublic XTIndHandlePtr xt_ind_get_handle(..)  
    Line 378: xtPublic void xt_ind_release_handle(XTIndHandlePtr..)  
    Line 516: xtPublic xtBool xt_ind_copy_on_write(XTIndReferencePtr iref)  
    Line 597: xtPublic void xt_ind_lock_handle(XTIndHandlePtr handle) 


Yes it meaning replace something with an empty string. But the important thing is now something is recognized by the preprocessor that it is "defined", so

#ifdef something

will pass after that #define (Line 172).

Also, it is common to use it for configurational or vendor-specific attributes (Line 325, ...), like

#if MSVC
#define EXPORT __declspec(dllexport)
#else
#define EXPORT
#endif

EXPORT void f();
// expand to '__declspec(dllexport) void f()' in MSVC
// expand to 'void f()' in other compilers


Those declarations are usually given within header files, as a means of preventing double inclusion of the same file. These are also called include guards.


#define something will result into something just defined. It will not cause a compiler error. It is used usually like

void getValue(IN int& x, OUT int& y). If you do #define IN and #define OUT it will not give a compiler error and anybody will get to know x is input and y is output

One more use is like

#ifndef __ABC_H__
#define __ABC_H__

...

#endif

This is to prevent reinclusion of for eg. "abc.h"


Its is nothing but Pre-Processor Directive, the #define just will direct the Header files to the considered Library files or can declares the constants.


Yes, it replaces the preprocessor with empty string. It helps is self-documenting the code without writing lengthy comments.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜