The problem resides below - I'm trying to make a directive command that disable one constructor definition, or the other, based on the parameter of T. When I uncomment the block around the if statement, I get this error-- ... missing binary operator before token "(" --but when I replace the ( typeid(T).name() == typeid(int).name() ) statement with something like 0 or true the code compiles just fine. Here's the code--

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

template<class T>class Enum;

template<class T>
class Enum
{ 
    public:
           struct Case;
/*
#if ( typeid(T).name() == typeid(int).name() )
#define CONSTRUCTOR
           Enum(){};
#else
#define CONSTRUCTOR
           Enum(){};
#endif*/

    private:
            Case *cases;

};

template<class T>
struct Enum<T>::Case
{
    T value;
    string name;
    static int total;

    Case(T theValue, const char *theName)
    : value(theValue), name(theName) {total++;};
};

template<>
struct Enum<int>::Case
{
    int value;
    string name;
    static int total;

    Case(const char *theName)
    : value(total), name(theName) {total++;};
};


int main(int argc, char *argv[])
{
    
    cout << ( typeid(int).name() == typeid(int).name() ) << endl;
    cin.get();
    return 0;
}

As you can see, I tested the statement in main and it works perfectly fine, returning a bool value after the comparison. I just don't understand why #if doesn't recognize it as a bool expression.

The #ifdef directive is a PRECOMPILER directive that comments out or uncomments out a block of code depending on some other condition. This is performed before the code is even compiled, so the actual compiler never sees the #ifdef directive. Nor is it affected at program executation time.

The condition must be satisfied before compile time, not during runtime. So what you are attempting to do on line 15 is just not possible because those variables are not known until runtime.

The condition must be satisfied before compile time, not during runtime. So what you are attempting to do on line 15 is just not possible because those variables are not known until runtime.

The type T itself is known before runtime, is it not? For example, if I instantiate said class with a parameter, the compiler will replace any occurance of T with the actual type, for the particular class/function.

I used the statement-- #if (T == int) --and the compiler didn't choke up on me.

I did some research and the #if directive states that the expression must be a constant expression. What exactly does that mean? I'm still having trouble understanding that.

Note: The above statement seems to always be true, even though I'm not really sure how the == operand is resolving the expression above.

In your example, both T and int are const expressions. (T).name() is not a const because it is only known at runtime.

Ok, now that was a very straightforward response. Thanks a million.

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.