Templates and Specialisation error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simon

    Templates and Specialisation error

    Hi,

    I'm having a problem with templates and specialisation. I'm using it to
    overload the same function so it can return different things. I can't see
    what I'm doing wrong, although my compiler promises me I am! Here follows an
    example of my code, and i've included the error VC is giving me if that
    helps. Can anyone point me in the right direction?
    Cheers,
    Simon ;o)
    --
    template <class A, class B>
    A foo(B x);

    template <>
    int foo(int x) {
    return x + 1;
    }

    template <>
    float foo(int x) {
    return (float)(x) + 1.0f;
    }

    int main () {
    int a = 1;
    int b = foo<int,int>(a) ;
    float c = foo<float,int>( a);
    return 1;
    }

    VC6 is giving...

    c:\test\main.cp p(13) : error C2556: 'float __cdecl foo(int)' : overloaded
    function differs only by return type from 'int __cdecl foo(int)'
    c:\test\main.cp p(8) : see declaration of 'foo'
    c:\test\main.cp p(13) : error C2371: 'foo' : redefinition; different basic
    types
    c:\test\main.cp p(8) : see declaration of 'foo'
    c:\test\main.cp p(21) : error C2893: Failed to specialize function template
    'A __cdecl foo(B)'
    With the following template arguments:
    'int'
    'int'
    Error executing cl.exe.



  • Mike Wahler

    #2
    Re: Templates and Specialisation error

    "Simon" <sorrynomail@no .no> wrote in message
    news:bkvoo0$lcl $1@titan.btinte rnet.com...[color=blue]
    > Hi,
    >
    > I'm having a problem with templates and specialisation. I'm using it to
    > overload the same function so it can return different things. I can't see
    > what I'm doing wrong, although my compiler promises me I am! Here follows[/color]
    an[color=blue]
    > example of my code, and i've included the error VC is giving me if that
    > helps. Can anyone point me in the right direction?
    > Cheers,
    > Simon ;o)
    > --
    > template <class A, class B>
    > A foo(B x);
    >
    > template <>
    > int foo(int x) {
    > return x + 1;
    > }
    >
    > template <>
    > float foo(int x) {[/color]

    Whoops! You cannot overload on return type alone.
    See below.
    [color=blue]
    > return (float)(x) + 1.0f;
    > }
    >
    > int main () {
    > int a = 1;
    > int b = foo<int,int>(a) ;
    > float c = foo<float,int>( a);
    > return 1;
    > }
    >
    > VC6 is giving...
    >
    > c:\test\main.cp p(13) : error C2556: 'float __cdecl foo(int)' : overloaded
    > function differs only by return type from 'int __cdecl foo(int)'[/color]

    This is an accurate diagnosis. Functinos cannot be
    overloaded on return type only. The parameter types
    and/or count must differ.
    [color=blue]
    > c:\test\main.cp p(8) : see declaration of 'foo'
    > c:\test\main.cp p(13) : error C2371: 'foo' : redefinition; different basic
    > types[/color]

    You're trying to define a function more than
    once, with a different return type. Not allowed.
    The parameter list must differ. Return type does
    not affect overloading.
    [color=blue]
    > c:\test\main.cp p(8) : see declaration of 'foo'
    > c:\test\main.cp p(21) : error C2893: Failed to specialize function template
    > 'A __cdecl foo(B)'
    > With the following template arguments:
    > 'int'
    > 'int'
    > Error executing cl.exe.[/color]

    -Mike


    Comment

    • Gianni Mariani

      #3
      Re: Templates and Specialisation error

      Simon wrote:[color=blue]
      > Hi,
      >
      > I'm having a problem with templates and specialisation. I'm using it to
      > overload the same function so it can return different things. I can't see
      > what I'm doing wrong, although my compiler promises me I am! Here follows an
      > example of my code, and i've included the error VC is giving me if that
      > helps. Can anyone point me in the right direction?
      > Cheers,
      > Simon ;o)[/color]

      VC6 is of a vintage that has alot of problems with templates.

      Get VC7.1

      Comment

      • WW

        #4
        Re: Templates and Specialisation error

        Gianni Mariani wrote:[color=blue]
        > Simon wrote:[color=green]
        >> Hi,
        >>
        >> I'm having a problem with templates and specialisation. I'm using it
        >> to overload the same function so it can return different things. I
        >> can't see what I'm doing wrong, although my compiler promises me I
        >> am! Here follows an example of my code, and i've included the error
        >> VC is giving me if that helps. Can anyone point me in the right
        >> direction?
        >> Cheers,
        >> Simon ;o)[/color]
        >
        > VC6 is of a vintage that has alot of problems with templates.
        >
        > Get VC7.1[/color]

        Other than that can you tell what rule does it get wrong in this case? Or
        just guessing?

        --
        WW aka Attila


        Comment

        • Mike Wahler

          #5
          Re: Templates and Specialisation error


          "Gianni Mariani" <gi2nospam@mari ani.ws> wrote in message
          news:bkvqgq$455 @dispatch.conce ntric.net...[color=blue]
          > Simon wrote:[color=green]
          > > Hi,
          > >
          > > I'm having a problem with templates and specialisation. I'm using it to
          > > overload the same function so it can return different things. I can't[/color][/color]
          see[color=blue][color=green]
          > > what I'm doing wrong, although my compiler promises me I am! Here[/color][/color]
          follows an[color=blue][color=green]
          > > example of my code, and i've included the error VC is giving me if that
          > > helps. Can anyone point me in the right direction?
          > > Cheers,
          > > Simon ;o)[/color]
          >
          > VC6 is of a vintage that has alot of problems with templates.[/color]

          Not in this case. The code is invalid.

          [color=blue]
          >
          > Get VC7.1[/color]

          That won't help (unless they broke it so it
          accepts the invalid code -- which isn't much of
          a solution. :-))

          -Mike


          Comment

          • WW

            #6
            Re: Templates and Specialisation error

            Mike Wahler wrote:[color=blue][color=green]
            >> VC6 is of a vintage that has alot of problems with templates.[/color]
            >
            > Not in this case. The code is invalid.
            >
            >[color=green]
            >>
            >> Get VC7.1[/color]
            >
            > That won't help (unless they broke it so it
            > accepts the invalid code -- which isn't much of
            > a solution. :-))[/color]

            I have copy pasted the code into Comeau online and it did not complain. Did
            I do something wrong? IMHO those might not be overloads (at least AIR they
            aren't) but full specializations . I might of course be wrong. That is why
            I have asked for standard text.

            --
            WW aka Attila


            Comment

            • Gianni Mariani

              #7
              Re: Templates and Specialisation error

              Mike Wahler wrote:[color=blue]
              > "Gianni Mariani" <gi2nospam@mari ani.ws> wrote in message
              > news:bkvqgq$455 @dispatch.conce ntric.net...
              >[/color]
              ....[color=blue][color=green]
              >>
              >>VC6 is of a vintage that has alot of problems with templates.[/color]
              >
              >
              > Not in this case. The code is invalid.
              >[/color]

              Then gcc 3.3.1 is broken. It compiled it without complaint.

              So who is right ?



              Comment

              • WW

                #8
                Re: Templates and Specialisation error

                Gianni Mariani wrote:[color=blue]
                > Mike Wahler wrote:[color=green]
                >> "Gianni Mariani" <gi2nospam@mari ani.ws> wrote in message
                >> news:bkvqgq$455 @dispatch.conce ntric.net...
                >>[/color]
                > ...[color=green][color=darkred]
                >>>
                >>> VC6 is of a vintage that has alot of problems with templates.[/color]
                >>
                >>
                >> Not in this case. The code is invalid.
                >>[/color]
                >
                > Then gcc 3.3.1 is broken. It compiled it without complaint.
                >
                > So who is right ?[/color]

                The standard. That is why I have asked for it. IMO the guessing you did is
                very dangerous. You have stated something and you do not really know if it
                is true or not. :-(

                I _think_ that if you fully specialize a template it does _not_ become an
                overload. Just think about name mangling (which is an implementation
                detail, but helps understanding). The mangled name of a function template
                specialization will contain all the names (or identifiers or whatevers) of
                the types used to specialize it. Including the dependent return type. Now
                IIRC MS 6 does it worng. It does not put into the mangled name types which
                are not part of the argument list. MS has tried there to mix overload
                resolution with templates. Which did not work:

                template <class T> void f() {
                #1
                }


                f<int>();
                f<std::string>( );

                Both will call f<int> or f<std::string> , depending on the current phase of
                the moon and some other factors.

                So it is highly likely that in this case MS is wrong and all the others are
                right. But... which part of the standard does it say so? Or is this a grey
                area? (I doubt it).

                --
                WW aka Attila


                Comment

                • Jerry Coffin

                  #9
                  Re: Templates and Specialisation error

                  In article <bkvoo0$lcl$1@t itan.btinternet .com>, sorrynomail@no. no
                  says...[color=blue]
                  > Hi,
                  >
                  > I'm having a problem with templates and specialisation. I'm using it to
                  > overload the same function so it can return different things.[/color]

                  You're not doing overloading -- you're doing explicit specialization.
                  If you tried to do this via overloading, it wouldn't be allowed. You
                  can't create overloads that differ only in return type, but you CAN
                  create explicit specializations that do so (though when you do so, the
                  compiler generally will NOT be able to deduce the type of the parameter
                  specifying the function's return type).
                  [color=blue]
                  > I can't see
                  > what I'm doing wrong, although my compiler promises me I am! Here follows an
                  > example of my code, and i've included the error VC is giving me if that
                  > helps. Can anyone point me in the right direction?[/color]

                  I believe the code is well-formed, and _should_ be accepted by a
                  properly functioning compiler. I'm not surprised that quite a few
                  compilers reject it though, and quite frankly, I'd tend to do the same.
                  IMO, if you use this, confusion is inevitable while utility is
                  questionable.

                  --
                  Later,
                  Jerry.

                  The universe is a figment of its own imagination.

                  Comment

                  • Simon

                    #10
                    Re: Templates and Specialisation error

                    > > I'm having a problem with templates and specialisation. I'm using it to[color=blue][color=green]
                    > > overload the same function so it can return different things.[/color]
                    >
                    > You're not doing overloading -- you're doing explicit specialization.
                    > If you tried to do this via overloading, it wouldn't be allowed. You
                    > can't create overloads that differ only in return type, but you CAN
                    > create explicit specializations that do so.[/color]

                    Yeah, sorry. I shouldn't really have used the word overload i guess. I'm
                    trying to use templates for exactly the reason you suggest, it just is meant
                    to feel like overloading. e.g. cast<template specialisation to>(overloaded
                    types from)
                    [color=blue]
                    > (though when you do so, the
                    > compiler generally will NOT be able to deduce the type of the parameter
                    > specifying the function's return type).[/color]

                    I don't understand this comment. Could you expand.
                    [color=blue][color=green]
                    > > I can't see
                    > > what I'm doing wrong, although my compiler promises me I am! Here[/color][/color]
                    follows an[color=blue][color=green]
                    > > example of my code, and i've included the error VC is giving me if that
                    > > helps. Can anyone point me in the right direction?[/color]
                    >
                    > I believe the code is well-formed, and _should_ be accepted by a
                    > properly functioning compiler.[/color]

                    So can we confirm that the original code was correct. There seem to be a
                    number of people saying it is incorrect, and if so I want to understand
                    where my misunderstandin g of C++/templates lies.

                    Thanks for all the help so far,
                    Simon ;o)


                    Comment

                    • tom_usenet

                      #11
                      Re: Templates and Specialisation error

                      On Thu, 25 Sep 2003 22:53:22 GMT, "Mike Wahler"
                      <mkwahler@mkwah ler.net> wrote:
                      [color=blue]
                      >
                      >"Gianni Mariani" <gi2nospam@mari ani.ws> wrote in message
                      >news:bkvqgq$45 5@dispatch.conc entric.net...[color=green]
                      >> Simon wrote:[color=darkred]
                      >> > Hi,
                      >> >
                      >> > I'm having a problem with templates and specialisation. I'm using it to
                      >> > overload the same function so it can return different things. I can't[/color][/color]
                      >see[color=green][color=darkred]
                      >> > what I'm doing wrong, although my compiler promises me I am! Here[/color][/color]
                      >follows an[color=green][color=darkred]
                      >> > example of my code, and i've included the error VC is giving me if that
                      >> > helps. Can anyone point me in the right direction?
                      >> > Cheers,
                      >> > Simon ;o)[/color]
                      >>
                      >> VC6 is of a vintage that has alot of problems with templates.[/color]
                      >
                      >Not in this case. The code is invalid.[/color]

                      It is perfectly valid. It uses complete specialization of function
                      templates, not overloading as you stated in your other post.

                      Primary template:
                      template <class A, class B>
                      A foo(B x);

                      Specialization foo<int, int>:
                      template <>
                      int foo(int x) {
                      return x + 1;
                      }

                      Specialization foo<float, int>:
                      template <>
                      float foo(int x) {
                      return (float)(x) + 1.0f;
                      }

                      The specializations would be better written as:

                      template <>
                      int foo<int, int>(int x);
                      and
                      template <>
                      float foo<float, int>(int x);

                      to make it clearer which primary template they are specializing
                      (although it is obvious in the original code).

                      Tom

                      Comment

                      • Jerry Coffin

                        #12
                        Re: Templates and Specialisation error

                        In article <bl0sm3$gs3$1@h ercules.btinter net.com>, sorrynomail@no. no
                        says...

                        [ ... ]
                        [color=blue][color=green]
                        > > (though when you do so, the
                        > > compiler generally will NOT be able to deduce the type of the parameter
                        > > specifying the function's return type).[/color]
                        >
                        > I don't understand this comment. Could you expand.[/color]

                        Sure -- typically with a templated function, the compiler can figure out
                        the type(s) to instantiate the function over from the types of the
                        arguments. E.g. with something like:

                        template<class T>
                        T increment(T const &t) {
                        return t+1;
                        }

                        I can simply write:

                        x = increment(2);

                        and the compiler figures out that since 2 is an int that it needs to
                        instantiate increment over type int.

                        With your function the compiler can't do that, so you always have to
                        explicitly specify the template parameter for the return type.

                        [ ... ]
                        [color=blue][color=green]
                        > > I believe the code is well-formed, and _should_ be accepted by a
                        > > properly functioning compiler.[/color]
                        >
                        > So can we confirm that the original code was correct. There seem to be a
                        > number of people saying it is incorrect, and if so I want to understand
                        > where my misunderstandin g of C++/templates lies.[/color]

                        Well, I don't know whether it qualifies as a confirmation or not, but
                        looking at things in the bright light of the morning, I'm still of the
                        same opinion -- I think the code is well-formed.

                        OTOH, the "bright light of morning" means the sun is shining in my eyes
                        right now and I can't see the screen as well as usual at the moment, so
                        that may not mean all that much. <G>

                        --
                        Later,
                        Jerry.

                        The universe is a figment of its own imagination.

                        Comment

                        • Simon

                          #13
                          Re: Templates and Specialisation error

                          > > > I believe the code is well-formed, and _should_ be accepted by a[color=blue][color=green][color=darkred]
                          > > > properly functioning compiler.[/color]
                          > >
                          > > So can we confirm that the original code was correct. There seem to be a
                          > > number of people saying it is incorrect, and if so I want to understand
                          > > where my misunderstandin g of C++/templates lies.[/color]
                          >
                          > Well, I don't know whether it qualifies as a confirmation or not, but
                          > looking at things in the bright light of the morning, I'm still of the
                          > same opinion -- I think the code is well-formed.
                          >
                          > OTOH, the "bright light of morning" means the sun is shining in my eyes
                          > right now and I can't see the screen as well as usual at the moment, so
                          > that may not mean all that much. <G>[/color]

                          I tried it in g++ and it worked fine, so I think this is a closed case.
                          MSVC6 seems very broken with regard to templates! I've had it doing some
                          other wierd stuff with them too.

                          Thanks for all your help ;o)



                          Comment

                          Working...