Question/issue on static const vars and #if directive

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2008
Posts: 31
Reputation: rmlopes is an unknown quantity at this point 
Solved Threads: 0
rmlopes rmlopes is offline Offline
Light Poster

Question/issue on static const vars and #if directive

 
0
  #1
Feb 20th, 2009
Hello all,

I am trying to use the #if directive to compile extra functionality acording to some class template parameter defined integer constant. What I want to do is this:

  1. struct A1{
  2. static const int _AINT = 0;
  3. };
  4.  
  5. struct A2{
  6. static const int _AINT = 1;
  7. };
  8.  
  9. template< class T >
  10. struct B{
  11. void Fun(){
  12. #if T::_AINT == 1
  13. cout << " TADA! "<< endl;
  14. #endif
  15. };
  16. };
  17.  
  18. int main(void){
  19. B< A1 > b1;
  20. B< A2 > b2;
  21. b1.Fun();
  22. b2.Fun();
  23. return 0;
  24. };

This compiles, I guess because the compiler recognizes the integer constant and accepts it. But when it runs it does not behave as (I) expected.
Can someone explain me why does this happen and maybe suggest some other way of doing it (avoiding an approach like the Strategy pattern)?

Thank you in advance
Rui
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,451
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 119
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Question/issue on static const vars and #if directive

 
0
  #2
Feb 20th, 2009
Why are you using the preprocessor for this?
This compiles and works for me:
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct A1 {
  5. static const int _AINT = 0;
  6. };
  7.  
  8. struct A2 {
  9. static const int _AINT = 1;
  10. };
  11.  
  12. template< class T >
  13. struct B{
  14. void Fun(){
  15. if ( T::_AINT == 1 ) {
  16. cout << "TADA!";
  17. }
  18. };
  19. };
  20.  
  21. int main(void){
  22. B< A1 > b1;
  23. B< A2 > b2;
  24. b1.Fun();
  25. b2.Fun();
  26. return 0;
  27. };
Last edited by William Hemsworth; Feb 20th, 2009 at 4:04 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,751
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: 740
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Question/issue on static const vars and #if directive

 
1
  #3
Feb 20th, 2009
>This compiles
Probably not cleanly. Check your warnings.

>Can someone explain me why does this happen
Aside from the fact that you're trying to access a run-time value at preprocessing time (ie. long before it exists)?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,451
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 119
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Question/issue on static const vars and #if directive

 
0
  #4
Feb 20th, 2009
... Forget this post
Last edited by William Hemsworth; Feb 20th, 2009 at 4:51 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 31
Reputation: rmlopes is an unknown quantity at this point 
Solved Threads: 0
rmlopes rmlopes is offline Offline
Light Poster

Re: Question/issue on static const vars and #if directive

 
0
  #5
Feb 23rd, 2009
Hello,

The example doens't show it all. I need to do this because there is code where I call a function that exists in A2 but not in A1 for instance. And if I have the if (run-time structure) it will not compile. Now, what I am trying to do is in COMPILE time, and not run-time like Narue suggested. Is this possible? top have pre-processing mixed with metaprogramming? After all, the static constant used is known by the compiler before running...
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,751
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: 740
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Question/issue on static const vars and #if directive

 
1
  #6
Feb 23rd, 2009
>I need to do this because there is code where I call
>a function that exists in A2 but not in A1 for instance.
Template specialization is what you want.

>After all, the static constant used is known by the compiler before running...
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. int x = 10;
  6.  
  7. #if x == 10
  8. std::cout<<"fizz\n";
  9. #else
  10. std::cout<<"buzz\n";
  11. #endif
  12. }
Why doesn't this work? Both x and the value it's initialized with are known by the compiler before running... Do you realize how silly your rationalization sounds?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 793
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Question/issue on static const vars and #if directive

 
1
  #7
Feb 23rd, 2009
Dude, #'s are Preprocessor directives. That means all of those things are done before compiling your code.
So here how it goes.
First, the preprocessor work on all the #'s and make the code ready for compilation
it doesnt even see your code. nor does the preprocessor knows c/c++.
It understands only macros i.e. all the #'s

After the preprocessor does its job, your code is then compiled by the compiler.

So getting the point?
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC