Solved

VC++ Compiler Bug?

Posted on 2000-05-17
9
326 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Lambda for random numbers problem 7 120
Unresolved External Symbols 3 129
operator overload: incompatible type and too many params 5 90
How do i run a c++ file? 15 51
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

756 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