473,657 Members | 2,537 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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;
}

SomeOtherFuncti on(...)
{
... 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 1938
"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;
}

SomeOtherFuncti on(...)
{
... 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;
}

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

MyFunc<double , long>(2, 5);
MyFunc<floa t, int>(3, 7);
MyFunc<shor t, 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.publi c.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
correction s 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.publi c.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
2434
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 I'm at a complete loss as to what is wrong with the one below. This is a very abbreviated server-config.wsdd: <?xml version="1.0" encoding="UTF-8"?> <deployment xmlns="http://xml.apache.org/axis/wsdd/"...
17
597
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
2722
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 the start that my knowledge of compiler implementation is very limited. Therefore, my suggestions may have to be rejected because they are difficult or impossible to implement. The proposal is based on the concept of "type similarity". Type...
6
6168
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 ok template <class T>
9
1486
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
1904
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); }; template<typename T>
2
2590
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 I'm doing something wrong and g++ just doesn't notice, or if the people at Intel are doing something weird... What am I trying to do? I'm trying to implement a template class that inherits from the Blitz++ array library #include <blitz/array.h>...
1
1420
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. things changed later, its used anywhere necessary in the code. and soon, I was bored up by chained (type*)(void*) conversion. so, I deciede to write a generic conversion operator, sth like this: class extentable_buffer { public:
6
391
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
8303
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8821
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8502
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8602
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7316
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1941
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.