开发者

How to check if a datatype is "defined" with typedef

I faced this problem today and just wondering how to check if a new type defined with typedef is really defined somewhere. To give an example, I started using Xerces-c3 library that I built from source code and wrote a xml2text converter. But I couldnt find Xerces-c3 port on fbsd so installed Xerces-c2 library.

When I tried to recompile my source code I got following error:

XML2Text.cc:83: error: cannot declare variable 'handler' to be of abstract type 'XML2TextHandlers'
XML2TextHandlers.h:32: note:   because the following virtual functions are pure within 'XML2TextHandlers':
/usr/local/include/xercesc/framework/XMLFormatter.hpp:454: note:  virtual void xercesc_2_7::XMLFormatTarget::writeChars(const XMLByte*, unsigned int, xercesc_2_7::XMLFormatter*)

I am using following definition in my header file for writeChars method

virtual void writeChars(const XMLByte* const toWrite,
                        const XMLSize_t count,
                        XMLFormatter* const formatter );

I checked that XMLSize_t is nothing but unsigned int declared with following:

#define XERCES_SIZE_T size_t  
typedef XERCES_SIZE_T XMLSize_t;

So if I want to make a code compatible to both the libraries how will I do it? One way I can think of is to check whether the version of the library and d开发者_如何学Cefine XMLSize_t accordingly. Any other way?

Thanks,

Shripad


There is no way to directly recognise whether a typedef is defined. The most popular workaround is to check if the file that defines the typedef also defines a macro.

e.g. The type "struct tm" is defined in time.h. If you look at your copy of time.h, there will be a macro defined at the top. In the VC2010 version it is "_INC_TIME" so you can write

#if !defined(_INC_TIME)
    // Do whatever
#endif

If the type you are interested in defines a macro, then you can check for that.


Not sure about how you would go about checking typedefs, but if theres a macro you can identify in the file with the typedef, you can check define statements with #ifdef

http://gcc.gnu.org/onlinedocs/cpp/Ifdef.html

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜