Link to home
Start Free TrialLog in
Avatar of jasonclarke
jasonclarke

asked on

VC++ Compiler Bug?

I think this code displays a problem with the VC++ 6 compiler.  Can somebody confirm this for me by trying this on another compiler?

Or if its not a compiler bug, can somebody let me know whats wrong with it? (More points may be on offer if this is the case).

template <class T>
void f(void*)
{}

template <class T>
class X
{
public:
    void init();
};

template <class T>
void X<T>::init()
{
    void (*cb)(void*);
    cb = f<T>;
}

int main(int argc, char* argv[])
{
    X<float> x;
    x.init();

    return 0;
}
Avatar of jasonclarke
jasonclarke

ASKER

BTW, The problem code is:

    void (*cb)(void*);
    cb = f<T>;

and if you replace it with (the hopefully equivalent!):

    void (*cb)(void*) = f<T>;

you get a different, but equally mystifying compiler error.


ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
that as supposed to be runs "fine", although it "fun" too.  :-)
Thanks,  I thought so.

There problem seems to be that VC++ can't instantiate a function template properly when you attempt to take the address of it if the template type does not appear in the argument list.

I can do a nasty workaround in this case because the argument is a pointer, i.e.: I can change it to:

template <class T>
void f(T*)
{}

template <class T>
void X<T>::init()
{
    void (*cb)(T*);
    cb = f<T>;

    void (*cb2)(void*) = reinterpret_cast<void(*)(void*)>(cb);
}


(BTW, the context is that I am trying to take the address of is a static callback function that casts its argument to the template type:

template <class T>
void callback(void* clientData)
{
   T* Callee = static_cast<T*>(clientData);
   Callee->callback();
}

the callback signature is fixed because it is whats required by a 3rd party library.
)



Templates are clearly the worst part of VC.  They took a bad tact early on and have been headed in the wrong direction.  (VC's templates are heavily linked with overloading, which might have been reasonable at the start of the template development, but now is a mistake.)  Maybe VC 7...
Maybe.  The worrying thing about VC 7, as far as I can tell, is that the info that there is doesn't seem to say anything about compiler improvements.  Everything seems to be related to 'web development'.
Not surprising.  MS tends to be before the forefront.  I wish they were in the present.
With VC7 the matter isn't all that better.
Yet there is a nice workaround, that too enforces you to write nice code.
Just to verify that the code realy executes the right way I'v tested for the call and the passed data.

#include <iostream.h>

template <class T>
void f(void* ptr)
{
     cout << "Data:" << ptr << endl;
}

template <class T>
class X
{
public:
     typedef void(*fptr)(void*);
   void init();
};

template <class T>
void X<T>::init()
{
   fptr cb = f<T>;
   cb( new int(5) );
}

int main(int argc, char* argv[])
{
   X<float> x;
   x.init();

   return 0;
}

I'v noticed that often template problems can be worked around with typedefs.
Thanks for the input.  But it is more than two years since this question was asked, when talk of VC7 was mere speculation (I note I was perhaps too cynical then, VC7 is a better C++ compiler, and I believe the next one will be very nearly ANSI compliant).  

I can't really even remember why I asked the question now.