473,326 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Testing for a type


Is there any way of testing at compile time (i.e. by the preprocessor)
whether a particular type has been declared?

I guess what I'm looking for is something like

#if defined(uint8_t)
....
#endif

although of course I know that particular construction doesn't work.
I've also tried

#if sizeof(uint8_t) == 1

but that doesn't work either

Thanks!

Mar 23 '07 #1
2 1371
bytebro wrote:
Is there any way of testing at compile time (i.e. by the preprocessor)
whether a particular type has been declared?
Not directly, no, because types don't come into existence
until after the preprocessor has finished. Keywords like `int'
and `typedef' are merely source tokens to the preprocessor; they
aren't even recognized as keywords until later in compilation.
I guess what I'm looking for is something like

#if defined(uint8_t)
...
#endif

although of course I know that particular construction doesn't work.
What is usually done is to #define a preprocessor macro in
the same header that declares the type(s) of interest:

typedef unsigned char uint8_t;
#define UINT8_T_DEFINED

.... and then you can test for the macro

#ifdef UINT8_T_DEFINED

Note that `uint8_t' is one of the types from <stdint.hin
C99-conforming implementations. If you are using <stdint.h(as
opposed to writing pre-C99 "free-hand" declarations of your own),
then the macro `UINT8_MAX' is defined if `uint8_t' exists.
I've also tried

#if sizeof(uint8_t) == 1

but that doesn't work either
Since types themselves don't exist during preprocessing, the
sizes of types also don't exist. (In fact, `sizeof' is not even
recognized as an operator this early in the game.)

--
Eric Sosman
es*****@acm-dot-org.invalid
Mar 23 '07 #2
On 23 Mar, 14:13, Eric Sosman <esos...@acm-dot-org.invalidwrote:
>
What is usually done is to #define a preprocessor macro in
the same header that declares the type(s) of interest:

typedef unsigned char uint8_t;
#define UINT8_T_DEFINED

... and then you can test for the macro

#ifdef UINT8_T_DEFINED

Note that `uint8_t' is one of the types from <stdint.hin
C99-conforming implementations. If you are using <stdint.h(as
I'm using gcc 2.95.3 on SunOS 5.6 :( The types are defined in
<inttypes.h(actually in <sys/int_types.h>), and sadly the per-type
defines you describe are not done.

Never mind; I'll put together some kind of configure script that sets
the necessary defines.

And thanks for the advice.

Mar 23 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Nathon Dalton | last post by:
I need to know what the appropriate way to test for nothing in several different types of data types is. Here are some examples For string I use this If Not abc.text Is Nothing AndAlso Not...
0
by: Jonathan Allen | last post by:
We have found that our method of testing does not match traditional text-book methodologies. I decided to write a short white paper on it so that I could get your opinions. Does anyone else use...
38
by: Christoph Zwerschke | last post by:
In August 2001, there was a thread about the "Art of Unit Testing": http://groups.google.com/group/comp.lang.python/browse_frm/thread/aa2bd17e7f995d05/71a29faf0a0485d5 Paul Moore asked the...
72
by: Jacob | last post by:
I have compiled a set og unit testing recommendations based on my own experience on the concept. Feedback and suggestions for improvements are appreciated: ...
44
by: John A. Bailo | last post by:
Dr. Dobbs has a /glowing/ article on Ruby on Rails this month. What do you guys think? Can it replace .net, php and java? And be the Open Source OOP web solution that is not bound to Sun or...
6
by: Dick | last post by:
I’d appreciate some advice regarding unit tests. My questions are general in nature but (as usual) best conveyed via a (simplified) example: I have a table that contains two columns – the...
18
by: Andrew Wan | last post by:
I have been developing web applications with ASP & Javascript for a long time. I have been using Visual Studio 2003.NET. While VS2003 is okay for intellisense of ASP & Javascript, it's still not...
1
by: iLL | last post by:
If I have a collection of polymorphic objects, is there a way to test exactly what data type an element of that collection is? I’ve though of two workarounds: 1. Type casting and testing...
11
by: VK | last post by:
In the continuation of the discussion at "Making Site Opaque -- This Strategy Feasible?" and my comment at http://groups.google.com/group/comp.lang.javascript/msg/b515a4408680e8e2 I have...
3
by: TompIfe | last post by:
Hei, I have a web service with web methods that take nullable DateTime parameters (i.e., DateTime?) When i open the service in IE7, I get the message that those methods cannot be viewed ("not...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.