Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

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 define XMLSize_t accordingly. Any other way?

Thanks,

Shripad

like image 221
Shripad Bodas Avatar asked Aug 18 '10 22:08

Shripad Bodas


People also ask

Is typedef a user-defined data type?

Typedef : C++ allows you to define explicitly new data type names by using the keyword typedef. Using typedef does not actually create a new data class, rather it defines a name for an existing type.

How can typedef be to define a type of structure?

The C language contains the typedef keyword to allow users to provide alternative names for the primitive (e.g.,​ int) and user-defined​ (e.g struct) data types. Remember, this keyword adds a new name for some existing data type but does not create a new type.

Is typedef the same as define?

Difference between typedef and #define:typedef is limited to giving symbolic names to types only, whereas #define can be used to define an alias for values as well, e.g., you can define 1 as ONE, 3.14 as PI, etc.

What is typedef explain with an example?

typedef is used to define new data type names to make a program more readable to the programmer. For example: | main() | main() { | { int money; | typedef int Pounds; money = 2; | Pounds money = 2 } | } These examples are EXACTLY the same to the compiler.


1 Answers

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.

like image 67
Michael J Avatar answered Oct 15 '22 04:10

Michael J