[Q] Generating compile time error when sizeof(int) != 4

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2004
Posts: 2
Reputation: mdos is an unknown quantity at this point 
Solved Threads: 0
mdos mdos is offline Offline
Newbie Poster

[Q] Generating compile time error when sizeof(int) != 4

 
0
  #1
Nov 5th, 2004
I was recently given a list of possible C++ interview questions by a recruiter for a firm on Wall St. One of the questions was:


How would you generate a compile time error if the sizeof(int) is not equal to 4?


I was thinking along the lines of template specialization or some such thing, but I'm not sure. Any ideas?

Thanks.

-Marc
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,846
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 753
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: [Q] Generating compile time error when sizeof(int) != 4

 
0
  #2
Nov 5th, 2004
You can do it with templates, but it really isn't a better approach complexity-wise. The common method is to use the preprocessor with <climits>:
  1. #include <climits>
  2. #include <iostream>
  3.  
  4. #if INT_MAX != 2147483647
  5. # error integers must be 32 bits long
  6. #endif
  7.  
  8. int main ( void )
  9. {
  10. std::cout<< INT_MAX <<std::endl;
  11.  
  12. return 0;
  13. }
Though you need to be careful because the calculation might make an unwarranted assumption as above, such as CHAR_BIT being 8. Sometimes the assumption is safe, other times it constitutes an error.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,451
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 250
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: [Q] Generating compile time error when sizeof(int) != 4

 
0
  #3
Nov 5th, 2004
How about this?
#include <stdio.h>

char dummy [ sizeof(int) == 4 ];

int main(void)
{
   return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,846
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 753
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: [Q] Generating compile time error when sizeof(int) != 4

 
0
  #4
Nov 5th, 2004
>How about this?
Cute, but it's not very informative, is it? Though you do meet the requirements for the question. I wish I had remembered that trick for my reply, it's cleaner.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 2
Reputation: mdos is an unknown quantity at this point 
Solved Threads: 0
mdos mdos is offline Offline
Newbie Poster

Re: [Q] Generating compile time error when sizeof(int) != 4

 
0
  #5
Nov 5th, 2004
Originally Posted by Narue
You can do it with templates, but it really isn't a better approach complexity-wise.
Could you demonstrate the template based way to do this? I can't seem to figure this out. Thanks!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,846
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 753
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: [Q] Generating compile time error when sizeof(int) != 4

 
0
  #6
Nov 5th, 2004
>Could you demonstrate the template based way to do this?
  1. template <bool>
  2. struct static_assert;
  3.  
  4. template <>
  5. struct static_assert<true> {
  6. };
  7.  
  8. int main()
  9. {
  10. static_assert<sizeof ( int ) == 4>();
  11. }
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC