| | |
Question/issue on static const vars and #if directive
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 31
Reputation:
Solved Threads: 0
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:
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
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)
struct A1{ static const int _AINT = 0; }; struct A2{ static const int _AINT = 1; }; template< class T > struct B{ void Fun(){ #if T::_AINT == 1 cout << " TADA! "<< endl; #endif }; }; int main(void){ B< A1 > b1; B< A2 > b2; b1.Fun(); b2.Fun(); return 0; };
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
•
•
Join Date: Mar 2008
Posts: 1,495
Reputation:
Solved Threads: 123
Why are you using the preprocessor for this?
This compiles and works for me:
This compiles and works for me:
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; struct A1 { static const int _AINT = 0; }; struct A2 { static const int _AINT = 1; }; template< class T > struct B{ void Fun(){ if ( T::_AINT == 1 ) { cout << "TADA!"; } }; }; int main(void){ B< A1 > b1; B< A2 > b2; b1.Fun(); b2.Fun(); return 0; };
Last edited by William Hemsworth; Feb 20th, 2009 at 4:04 pm.
•
•
Join Date: Sep 2008
Posts: 31
Reputation:
Solved Threads: 0
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...
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...
>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...
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?
>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)
#include <iostream> int main() { int x = 10; #if x == 10 std::cout<<"fizz\n"; #else std::cout<<"buzz\n"; #endif }
New members chased away this month: 5
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?
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)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
![]() |
Other Threads in the C++ Forum
- Previous Thread: Capitalization Problem
- Next Thread: Problem with encoder and decoder. Please help!
Views: 363 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






