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:

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

Recommended Answers

All 6 Replies

Why are you using the preprocessor for this?
This compiles and works for me:

#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;
};

>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)?

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...

>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...

#include <iostream>

int main()
{
  int x = 10;

#if x == 10
  std::cout<<"fizz\n";
#else
  std::cout<<"buzz\n";
#endif
}

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?

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?

commented: The only post that would have been worth at the time, in this thread +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.