Solved

VC++ Compiler Bug?

Posted on 2000-05-17
9
324 Views
Last Modified: 2010-04-02
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;
}
0
Comment
Question by:jasonclarke
  • 4
  • 4
9 Comments
 
LVL 9

Author Comment

by:jasonclarke
ID: 2816543
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.


0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 2816613
It compiles and runs fun--as far as I can tell it runs fine--under BCB 4.  But doesn not copmpile under VC 6.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2816621
that as supposed to be runs "fine", although it "fun" too.  :-)
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 9

Author Comment

by:jasonclarke
ID: 2816674
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.
)



0
 
LVL 22

Expert Comment

by:nietod
ID: 2816893
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...
0
 
LVL 9

Author Comment

by:jasonclarke
ID: 2816975
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'.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2817511
Not surprising.  MS tends to be before the forefront.  I wish they were in the present.
0
 
LVL 5

Expert Comment

by:EOL
ID: 7466166
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.
0
 
LVL 9

Author Comment

by:jasonclarke
ID: 7468279
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.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question