Solved

VC++ Compiler Bug?

Posted on 2000-05-17
9
327 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

752 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