template problem in MSVC6 - getting wrong parameter type

Discussion in 'C++' started by christian, Jan 14, 2006.

  1. christian

    christian Guest

    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
     
    christian, Jan 14, 2006
    #1
    1. Advertising

  2. christian

    John Carson Guest

    "christian" <> wrote in message
    news:dq9ul5$p2e$-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
     
    John Carson, Jan 14, 2006
    #2
    1. Advertising

  3. christian

    Ian Guest

    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
     
    Ian, Jan 14, 2006
    #3
  4. christian

    christian Guest

    John Carson wrote:
    > "christian" <> wrote in message
    > news:dq9ul5$p2e$-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
     
    christian, Jan 14, 2006
    #4
  5. christian

    John Carson Guest

    "christian" <> wrote in message
    news:dqam6m$kf$-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
     
    John Carson, Jan 14, 2006
    #5
  6. christian

    christian Guest

    John Carson wrote:
    > "christian" <> wrote in message
    > news:dqam6m$kf$-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
     
    christian, Jan 14, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom Richardson
    Replies:
    1
    Views:
    328
    Victor Bazarov
    Dec 19, 2003
  2. John
    Replies:
    3
    Views:
    350
    Kevin Goodsell
    Apr 4, 2004
  3. Replies:
    2
    Views:
    430
  4. hamishd
    Replies:
    1
    Views:
    284
    Victor Bazarov
    Aug 31, 2006
  5. Stuart Redmann
    Replies:
    5
    Views:
    482
    Stuart Redmann
    Dec 14, 2007
Loading...

Share This Page