| | |
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:
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,427
Reputation:
Solved Threads: 115
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 }
I'm here to prove you wrong.
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)
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)
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!
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






