Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > template problem in MSVC6 - getting wrong parameter type

Reply
Thread Tools

template problem in MSVC6 - getting wrong parameter type

 
 
christian
Guest
Posts: n/a
 
      01-14-2006
Hi!

I have a problem with a template function im MSVC6 the template function
is defined as:

template <__Type1, __Type2> int MyFunc(int param1, double param2)
{__Type1 var1;
__Type2 var2;

... do something ...

return 0;
}

SomeOtherFunction(...)
{
... some different calls of MyFunc e.g.:

MyFunc<double, long>(2, 5);
MyFunc<float, int>(3, 7);
MyFunc<short, char>(1, 2);

... do other stuff
}

When the function MyFunc is called the variable var1 has the correct
type as specified in the function call, but the type of the variable
var2 is wrong ... leading to some pointer cast problems

Is something wrong with my implementation or is it a bug of MS?
Greetings Christian
 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      01-14-2006
"christian" <(E-Mail Removed)> wrote in message
news:dq9ul5$p2e$(E-Mail Removed)-stuttgart.de
> Hi!
>
> I have a problem with a template function im MSVC6 the template
> function is defined as:
>
> template <__Type1, __Type2> int MyFunc(int param1, double param2)
> {__Type1 var1;
> __Type2 var2;
>
> ... do something ...
>
> return 0;
> }
>
> SomeOtherFunction(...)
> {
> ... some different calls of MyFunc e.g.:
>
> MyFunc<double, long>(2, 5);
> MyFunc<float, int>(3, 7);
> MyFunc<short, char>(1, 2);
>
> ... do other stuff
> }
>
> When the function MyFunc is called the variable var1 has the correct
> type as specified in the function call, but the type of the variable
> var2 is wrong ... leading to some pointer cast problems
>
> Is something wrong with my implementation or is it a bug of MS?
> Greetings Christian



Show us complete, compilable code that illustrates the problem (you should
copy and paste directly from your compiler; do not retype it). What you have
shown us won't compile and, if the obvious corrections are made so it does
compile, then the problem you are talking about is not discernable from the
code you have supplied.

Incidentally, identifiers with double underscores are reserved for the
implementation to use. You should never use them in your own code.

--
John Carson


 
Reply With Quote
 
 
 
 
Ian
Guest
Posts: n/a
 
      01-14-2006
christian wrote:
> Hi!
>
> I have a problem with a template function im MSVC6 the template function
> is defined as:
>
> template <__Type1, __Type2> int MyFunc(int param1, double param2)


This isn't legal and wouldn't compile, is it a real example?

Ian
 
Reply With Quote
 
christian
Guest
Posts: n/a
 
      01-14-2006
John Carson wrote:
> "christian" <(E-Mail Removed)> wrote in message
> news:dq9ul5$p2e$(E-Mail Removed)-stuttgart.de
>
>>Hi!
>>
>>I have a problem with a template function im MSVC6 the template
>>function is defined as:
>>
>>template <__Type1, __Type2> int MyFunc(int param1, double param2)
>>{__Type1 var1;
>> __Type2 var2;
>>
>>... do something ...
>>
>>return 0;
>>}
>>
>>SomeOtherFunction(...)
>>{
>>... some different calls of MyFunc e.g.:
>>
>>MyFunc<double, long>(2, 5);
>>MyFunc<float, int>(3, 7);
>>MyFunc<short, char>(1, 2);
>>
>>... do other stuff
>>}
>>
>>When the function MyFunc is called the variable var1 has the correct
>>type as specified in the function call, but the type of the variable
>>var2 is wrong ... leading to some pointer cast problems
>>
>>Is something wrong with my implementation or is it a bug of MS?
>>Greetings Christian

>
>
>
> Show us complete, compilable code that illustrates the problem (you should
> copy and paste directly from your compiler; do not retype it). What you have
> shown us won't compile and, if the obvious corrections are made so it does
> compile, then the problem you are talking about is not discernable from the
> code you have supplied.
>
> Incidentally, identifiers with double underscores are reserved for the
> implementation to use. You should never use them in your own code.
>

Hi!

Here is a compilable example:

#include <windows.h>
#include <iostream.h>

template <class Type1, class Type2> int MyFunc(int a, long b)
{Type1 *t1;
Type2 *t2;

t1 = new (Type1);
*t1 = 1;
t2 = new (Type2);
*t2 = 2;

delete (t1);
delete (t2);

return 0;
}

int main(int argc, void *argv[]){
int ret;

ret = MyFunc<double, long>(2.0, 3);
ret = MyFunc<short, float>(3, 1.0);
ret = MyFunc<double, short>(1, 4.2);
ret = MyFunc<short, long>(1, -1);

return 0;
}

When I run it (MSVC6) with the debugger and set a breakpoint in MyFunc,
Type1 is always short and Type2 is always long as in the last function call.

Best regards christian
 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      01-14-2006
"christian" <(E-Mail Removed)> wrote in message
news:dqam6m$kf$(E-Mail Removed)-stuttgart.de
> John Carson wrote:
>>
>> Show us complete, compilable code that illustrates the problem (you
>> should copy and paste directly from your compiler; do not retype
>> it). What you have shown us won't compile and, if the obvious
>> corrections are made so it does compile, then the problem you are
>> talking about is not discernable from the code you have supplied.
>>
>> Incidentally, identifiers with double underscores are reserved for
>> the implementation to use. You should never use them in your own
>> code.

> Hi!
>
> Here is a compilable example:
>
> #include <windows.h>
> #include <iostream.h>
>
> template <class Type1, class Type2> int MyFunc(int a, long b)
> {Type1 *t1;
> Type2 *t2;
>
> t1 = new (Type1);
> *t1 = 1;
> t2 = new (Type2);
> *t2 = 2;
>
> delete (t1);
> delete (t2);
>
> return 0;
> }
>
> int main(int argc, void *argv[]){
> int ret;
>
> ret = MyFunc<double, long>(2.0, 3);
> ret = MyFunc<short, float>(3, 1.0);
> ret = MyFunc<double, short>(1, 4.2);
> ret = MyFunc<short, long>(1, -1);
>
> return 0;
> }
>
> When I run it (MSVC6) with the debugger and set a breakpoint in
> MyFunc, Type1 is always short and Type2 is always long as in the last
> function call.
> Best regards christian



OK. I am remembering now. This is a limitation of VC++ 6, which is generally
poor with templates.

Basically, VC++ 6 doesn't cope well when template parameters (Type1 and
Type2 in this case) are not included as function parameter types (int and
long in this case). If you changed the function signature to

template <class Type1, class Type2> int MyFunc(Type1 a, Type2 b)

then you would get the correct Type1 and Type2 types (which is rather
surprising, given that your third function call would then involve supplying
4.2 as a short). But I guess it is just a bug, so there is not much rhyme or
reason to it.

Your original code works fine on VC++ 2003 (after #include <iostream.h> has
been replaced by #include <iostream>, as now required by the C++ standard).

VC++ 6 is pretty much a nightmare if you want to make serious use of
templates. I suggest you look into upgrade possibilities (one such is VC++
2005 Express, which you can get for free).

For future reference

microsoft.public.vc.language

is the best newsgroup for anything VC++ specific.

--
John Carson



 
Reply With Quote
 
christian
Guest
Posts: n/a
 
      01-14-2006
John Carson wrote:
> "christian" <(E-Mail Removed)> wrote in message
> news:dqam6m$kf$(E-Mail Removed)-stuttgart.de
>
>>John Carson wrote:
>>
>>>Show us complete, compilable code that illustrates the problem (you
>>>should copy and paste directly from your compiler; do not retype
>>>it). What you have shown us won't compile and, if the obvious
>>>corrections are made so it does compile, then the problem you are
>>>talking about is not discernable from the code you have supplied.
>>>
>>>Incidentally, identifiers with double underscores are reserved for
>>>the implementation to use. You should never use them in your own
>>>code.

>>
>>Hi!
>>
>>Here is a compilable example:
>>
>>#include <windows.h>
>>#include <iostream.h>
>>
>>template <class Type1, class Type2> int MyFunc(int a, long b)
>>{Type1 *t1;
>> Type2 *t2;
>>
>>t1 = new (Type1);
>>*t1 = 1;
>>t2 = new (Type2);
>>*t2 = 2;
>>
>>delete (t1);
>>delete (t2);
>>
>>return 0;
>>}
>>
>>int main(int argc, void *argv[]){
>>int ret;
>>
>>ret = MyFunc<double, long>(2.0, 3);
>>ret = MyFunc<short, float>(3, 1.0);
>>ret = MyFunc<double, short>(1, 4.2);
>>ret = MyFunc<short, long>(1, -1);
>>
>>return 0;
>>}
>>
>>When I run it (MSVC6) with the debugger and set a breakpoint in
>>MyFunc, Type1 is always short and Type2 is always long as in the last
>>function call.
>>Best regards christian

>
>
>
> OK. I am remembering now. This is a limitation of VC++ 6, which is generally
> poor with templates.
>
> Basically, VC++ 6 doesn't cope well when template parameters (Type1 and
> Type2 in this case) are not included as function parameter types (int and
> long in this case). If you changed the function signature to
>
> template <class Type1, class Type2> int MyFunc(Type1 a, Type2 b)
>
> then you would get the correct Type1 and Type2 types (which is rather
> surprising, given that your third function call would then involve supplying
> 4.2 as a short). But I guess it is just a bug, so there is not much rhyme or
> reason to it.
>
> Your original code works fine on VC++ 2003 (after #include <iostream.h> has
> been replaced by #include <iostream>, as now required by the C++ standard).
>
> VC++ 6 is pretty much a nightmare if you want to make serious use of
> templates. I suggest you look into upgrade possibilities (one such is VC++
> 2005 Express, which you can get for free).
>
> For future reference
>
> microsoft.public.vc.language
>
> is the best newsgroup for anything VC++ specific.
>

Thanks a lot!
Well I'm just "upgrading" the project from c to c++ and as there's a lot
of old stuff in it and lot of people using msvc6 in my working place ...
we'll see. For the time being I'll use some dummy parameters
regards christian
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using declaration inside first template parameter as default valuefor second template parameter. Stuart Redmann C++ 5 12-14-2007 08:42 AM
template class instantiate without template parameter, automatic type deduction Fei Liu C++ 4 10-26-2007 02:39 PM
Another stupid MSVC6 linking problem hamishd C++ 1 08-31-2006 12:06 PM
Forwrard References in MSVC6 vs. Borland 5.5 compilter tools. John C++ 3 04-04-2004 07:45 PM
template specialization hides other specializations-msvc6 Tom Richardson C++ 1 12-19-2003 05:05 PM



Advertisments