template problem in MSVC6 - getting wrong parameter type

C

christian

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
 
J

John Carson

christian said:
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.
 
I

Ian

christian said:
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
 
C

christian

John said:
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
 
J

John Carson

christian said:
John said:
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.
 
C

christian

John said:
John said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top