Compiler not recognizing myArray[0];

M

Mike B

In the GNU g++ compiler, I can't get it to recognize the following as a
compiler error:

typedef bool integerSizeCheck [sizeof(int) == 0];

Shouldn't the compiler not allow me to do this? Is there some option I
can turn on to allow me to use this style of "compile-time assert"?
 
I

Ivan Vecerina

Mike B said:
In the GNU g++ compiler, I can't get it to recognize the following as a
compiler error:

typedef bool integerSizeCheck [sizeof(int) == 0];

Shouldn't the compiler not allow me to do this? Is there some option I
can turn on to allow me to use this style of "compile-time assert"?

For the g++ option that will make a zero-size array, you should
ask on a g++ group/mailing list.

But the following might fail in a more portable/reliable way:
typedef bool integerSizeCheck[-1+2*( sizeof(int)==0 )];
If it still does not fail as expected, the following might do:
typedef struct { char compile_check[2*!!(expr)-1]; } integerSizeCheck;


hth -Ivan
 
R

Rapscallion

Mike said:
In the GNU g++ compiler, I can't get it to recognize the following as a
compiler error:

typedef bool integerSizeCheck [sizeof(int) == 0];

Shouldn't the compiler not allow me to do this? Is there some option I
can turn on to allow me to use this style of "compile-time assert"?


#define CT_ASSERT(expr) typedef char CT_Check [(expr)?1:-1];

CT_ASSERT(sizeof(int) == 0)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top