473,320 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
Jan 14 '06 #1
5 1929
"christian" <ch*******@web.de> wrote in message
news:dq**********@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
Jan 14 '06 #2
Ian
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
Jan 14 '06 #3
John Carson wrote:
"christian" <ch*******@web.de> wrote in message
news:dq**********@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
Jan 14 '06 #4
"christian" <ch*******@web.de> wrote in message
news:dq*********@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

Jan 14 '06 #5
John Carson wrote:
"christian" <ch*******@web.de> wrote in message
news:dq*********@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
Jan 14 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Kevin Dean | last post by:
I'm trying to create an XSL transformation that will strip out development-specific attributes from deployment descriptors and other XML files. I have already successfully done so with web.xml but...
17
by: Alexander Stippler | last post by:
Hi, what do I have to do to get this (incorrect) piece of code to work. The specialization is wrong, but how can I do it? template <typename T, typename V> class Mask { public: Mask(int i)...
4
by: wkaras | last post by:
I would like to propose the following changes to the C++ Standard, the goal of which are to provide an improved ability to specify the constraints on type parameters to templates. Let me say from...
6
by: Floogle | last post by:
How come I get a "error C2062: type 'int' unexpected" error with the second version? Is it possible to achieve the same functiuonaility as the fn as a class method? Many thanks //this is...
9
by: Floogle | last post by:
I cannot figure out why the following program instantiates two type "2" instances instead of one "1" and one "2". Could someone plz enlighten me? Many thanks Here's the code ...
4
by: StephQ | last post by:
According to: http://www.parashift.com/c++-faq-lite/templates.html#faq-35.4 , if my understanding is correct, in template<typename T> class Foo { friend void func (const Foo<T>& foo); }; ...
2
by: aitrob | last post by:
Hi, I have a problem concerning templates/inheritance. I have a code that compiles fine with g++ 4.0.1 (Apple version), but gives a lot of errors with Intel C++ 10.1 (Mac OS X). I'm not sure if...
1
by: unkstar | last post by:
I've written a extentable_buffer class, which allows me to acquire bigger buffer whenever I need. The original version has a conversion operator to void*, it's enought for that good old time....
6
by: Gaijinco | last post by:
I'm trying to do a template class Node. My node.hpp is: #ifndef _NODE_HPP_ #define _NODE_HPP_ namespace com { namespace mnya { namespace carlos { template <typename T>
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.