943,600 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 492
  • C++ RSS
Feb 20th, 2009
0

Question/issue on static const vars and #if directive

Expand Post »
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:

cpp Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Light Poster
rmlopes is offline Offline
31 posts
since Sep 2008
Feb 20th, 2009
0

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

Why are you using the preprocessor for this?
This compiles and works for me:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Feb 20th, 2009
1

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

>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)?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Feb 20th, 2009
0

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

... Forget this post
Last edited by William Hemsworth; Feb 20th, 2009 at 4:51 pm.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Feb 23rd, 2009
0

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

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...
Reputation Points: 10
Solved Threads: 0
Light Poster
rmlopes is offline Offline
31 posts
since Sep 2008
Feb 23rd, 2009
1

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

>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...
C++ Syntax (Toggle Plain Text)
  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?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Feb 23rd, 2009
1

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

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?
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Capitalization Problem
Next Thread in C++ Forum Timeline: Problem with encoder and decoder. Please help!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC