Solved

VC++ Compiler Bug?

Posted on 2000-05-17
9
321 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now