A question on function overloading

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

    #1

    A question on function overloading

    Hi All,
    I was going through the concept of function overloading and
    the different ways of doing it. I thought overloading by return type
    could also have been implemented if the creater wanted that. I wanted
    to know why this is not part of c++?

    Thanks,
    Madhav.

  • Alf P. Steinbach

    #2
    Re: A question on function overloading

    * Madhav:[color=blue]
    > Hi All,
    > I was going through the concept of function overloading and
    > the different ways of doing it. I thought overloading by return type
    > could also have been implemented if the creater wanted that. I wanted
    > to know why this is not part of c++?[/color]

    It was probably seen as impractical.


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?

    Comment

    • Madhav

      #3
      Re: A question on function overloading

      >> I thought overloading by return type[color=blue][color=green]
      > > could also have been implemented if the creater wanted that. I wanted
      > > to know why this is not part of c++?[/color]
      >
      > It was probably seen as impractical.
      >[/color]
      may I ask why it was probably seen as impractical?

      Comment

      • Clem.Dickey@gmail.com

        #4
        Re: A question on function overloading

        Madhav wrote:
        [color=blue]
        > may I ask why it was probably seen as impractical?[/color]

        int f();
        double f();
        void g(double);
        void g(int);

        g(f());

        Which versions of f and g do you use?

        Comment

        • kayvcai@gmail.com

          #5
          Re: A question on function overloading

          just take these two function for example

          int f(){return 10;}
          char f(){return 'A';}

          if you have such codes in function main()

          f();

          How can the complier distinguish them?
          So overloading by returning different type is impossible

          Comment

          • Alf P. Steinbach

            #6
            Re: A question on function overloading

            * kayvcai@gmail.c om:[color=blue]
            > just take these two function for example
            >
            > int f(){return 10;}
            > char f(){return 'A';}
            >
            > if you have such codes in function main()
            >
            > f();
            >
            > How can the complier distinguish them?
            > So overloading by returning different type is impossible[/color]

            Not impossible, but a tad impractical.

            There already is syntax for selecting a function based solely on the
            result type, namely disambiguating &f by using a cast.

            Cheers,

            - Alf

            --
            A: Because it messes up the order in which people normally read text.
            Q: Why is it such a bad thing?
            A: Top-posting.
            Q: What is the most annoying thing on usenet and in e-mail?

            Comment

            • Mark P

              #7
              Re: A question on function overloading

              Alf P. Steinbach wrote:[color=blue]
              > * kayvcai@gmail.c om:[color=green]
              >> just take these two function for example
              >>
              >> int f(){return 10;}
              >> char f(){return 'A';}
              >>
              >> if you have such codes in function main()
              >>
              >> f();
              >>
              >> How can the complier distinguish them?
              >> So overloading by returning different type is impossible[/color]
              >
              > Not impossible, but a tad impractical.
              >
              > There already is syntax for selecting a function based solely on the
              > result type, namely disambiguating &f by using a cast.
              >[/color]

              What's the syntax for that? Is it,

              static_cast<cha r (*)()>(f)();

              Comment

              • Alf P. Steinbach

                #8
                Re: A question on function overloading

                * Mark P:[color=blue]
                > Alf P. Steinbach wrote:[color=green]
                >> * kayvcai@gmail.c om:[color=darkred]
                >>> just take these two function for example
                >>>
                >>> int f(){return 10;}
                >>> char f(){return 'A';}
                >>>
                >>> if you have such codes in function main()
                >>>
                >>> f();
                >>>
                >>> How can the complier distinguish them?
                >>> So overloading by returning different type is impossible[/color]
                >>
                >> Not impossible, but a tad impractical.
                >>
                >> There already is syntax for selecting a function based solely on the
                >> result type, namely disambiguating &f by using a cast.[/color]
                >
                > What's the syntax for that? Is it,
                >
                > static_cast<cha r (*)()>(f)();[/color]

                Looks OK, except of course that without arguments in the function
                signature, such overloads cannot currently be defined...

                A number of examples of function overload disambiguation is given by
                ยง13.4/4.

                A common technique for defining overloads that seemingly differ only in
                the result type is to use a dummy pointer argument with default value 0.
                That does not work with contextual disambiguation like a cast for the
                purpose of calling the function, because the disambiguation requries the
                full function signature, so the call must then supply an explicit
                argument. However, "overloads" that actually do differ only in the
                return type can be defined as template specializations , and then you can
                write just f<char>().

                --
                A: Because it messes up the order in which people normally read text.
                Q: Why is it such a bad thing?
                A: Top-posting.
                Q: What is the most annoying thing on usenet and in e-mail?

                Comment

                • Jerry Coffin

                  #9
                  Re: A question on function overloading

                  In article <1142225345.107 530.114510
                  @v46g2000cwv.go oglegroups.com> , madhav.kelkar@g mail.com
                  says...[color=blue]
                  > Hi All,
                  > I was going through the concept of function overloading and
                  > the different ways of doing it. I thought overloading by return type
                  > could also have been implemented if the creater wanted that. I wanted
                  > to know why this is not part of c++?[/color]

                  Consider what happens if you ignore the returned value:

                  int x() {}
                  char x() {}

                  int main() {
                  x();
                  return 0;
                  }

                  Which one should be called? There are also major problems
                  dealing with implicit conversions, such as:

                  double d = x();

                  calling one of the functions above.

                  --
                  Later,
                  Jerry.

                  The universe is a figment of its own imagination.

                  Comment

                  Working...