Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   template problem in MSVC6 - getting wrong parameter type (http://www.velocityreviews.com/forums/t451085-template-problem-in-msvc6-getting-wrong-parameter-type.html)

christian 01-14-2006 04:28 AM

template problem in MSVC6 - getting wrong parameter type
 
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

John Carson 01-14-2006 05:37 AM

Re: template problem in MSVC6 - getting wrong parameter type
 
"christian" <ch-kohler@web.de> wrote in message
news:dq9ul5$p2e$1@infosun2.rus.uni-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



Ian 01-14-2006 06:05 AM

Re: template problem in MSVC6 - getting wrong parameter type
 
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

christian 01-14-2006 11:10 AM

Re: template problem in MSVC6 - getting wrong parameter type
 
John Carson wrote:
> "christian" <ch-kohler@web.de> wrote in message
> news:dq9ul5$p2e$1@infosun2.rus.uni-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

John Carson 01-14-2006 01:20 PM

Re: template problem in MSVC6 - getting wrong parameter type
 
"christian" <ch-kohler@web.de> wrote in message
news:dqam6m$kf$1@infosun2.rus.uni-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




christian 01-14-2006 02:38 PM

Re: template problem in MSVC6 - getting wrong parameter type
 
John Carson wrote:
> "christian" <ch-kohler@web.de> wrote in message
> news:dqam6m$kf$1@infosun2.rus.uni-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


All times are GMT. The time now is 03:21 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.