return type deduction for a function template

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • C++Liliput

    #1

    return type deduction for a function template

    Consider the following code

    template<class T1, class T2>
    T2 sum(T1 a, T1 b)
    {
    T2 ret = a + b;
    return ret;
    }

    int main()
    {
    float f1 = 2, f2 = 3;
    int i1 = 2, i2 = 4;

    sum<float>(f1, f2); //ERROR
    float fret = sum<float>(f1, f2); //ERROR
    }

    Both the statements marked as "ERROR" throw errors during compilation.
    Now I can understand the first error in that a return type cannot be
    possibly deduced. But what I don't understand is the second error. Why
    is the return type not automatically deduced to be float? Such a
    deduction happens for arguments of a function template (in case we
    don't specify the data type during template instantiation directly).
    Then why cannot it happen for the return type? Is it a compiler bug or
    was it intended to be the behavior? If so why? I am using g++ compiler
    on Linux.
  • SG

    #2
    Re: return type deduction for a function template

    On 24 Okt., 07:05, "C++Liliput " <aveekmi...@gma il.comwrote:
    Consider the following code
    >
    template<class T1, class T2>
    T2 sum(T1 a, T1 b)
    {
       T2 ret = a + b;
       return ret;
    >
    }
    >
    int main()
    {
       float f1 = 2, f2 = 3;
       int i1 = 2, i2 = 4;
    >
       sum<float>(f1, f2); //ERROR
       float fret = sum<float>(f1, f2); //ERROR
     }
    There *is* no type deduction for the return type. But you may do the
    following:

    template<class T2, class T1// <-- swapped T1 & T2
    T2 sum(T1 a, T1 b)
    {
    T2 ret = a + b;
    return ret;
    }

    int main()
    {
    float f1 = 2, f2 = 3;

    sum<float>(f1, f2); //OK, T2=float, T1=float (deduced)
    }

    Cheers,
    SG

    Comment

    • Hendrik Schober

      #3
      Re: return type deduction for a function template

      C++Liliput wrote:
      Consider the following code
      >
      template<class T1, class T2>
      T2 sum(T1 a, T1 b)
      {
      T2 ret = a + b;
      return ret;
      }
      >
      int main()
      {
      float f1 = 2, f2 = 3;
      int i1 = 2, i2 = 4;
      >
      sum<float>(f1, f2); //ERROR
      float fret = sum<float>(f1, f2); //ERROR
      }
      >
      Both the statements marked as "ERROR" throw errors during compilation.
      Now I can understand the first error in that a return type cannot be
      possibly deduced. But what I don't understand is the second error. Why
      is the return type not automatically deduced to be float? Such a
      deduction happens for arguments of a function template (in case we
      don't specify the data type during template instantiation directly).
      Then why cannot it happen for the return type? Is it a compiler bug or
      was it intended to be the behavior? If so why? I am using g++ compiler
      on Linux.
      I'm afraid the answer is "because the language was designed to
      not to support this". (That it's not impossible to do this can
      be seen in other languages.)

      Not that commonly it is better to put ineducable template
      arguments before those that can be deduced:

      template< typename R, typename T >
      R sum(T,T);

      That way you can still specify the return value but rely on the
      compiler to deduce the argument type.

      Schobi

      Comment

      • James Kanze

        #4
        Re: return type deduction for a function template

        On Oct 24, 9:08 am, Hendrik Schober <spamt...@gmx.d ewrote:
        C++Liliput wrote:
        Consider the following code
        template<class T1, class T2>
        T2 sum(T1 a, T1 b)
        {
        T2 ret = a + b;
        return ret;
        }
        int main()
        {
        float f1 = 2, f2 = 3;
        int i1 = 2, i2 = 4;
        sum<float>(f1, f2); //ERROR
        float fret = sum<float>(f1, f2); //ERROR
        }
        Both the statements marked as "ERROR" throw errors during
        compilation. Now I can understand the first error in that a
        return type cannot be possibly deduced. But what I don't
        understand is the second error. Why is the return type not
        automatically deduced to be float? Such a deduction happens
        for arguments of a function template (in case we don't
        specify the data type during template instantiation
        directly). Then why cannot it happen for the return type?
        Is it a compiler bug or was it intended to be the behavior?
        If so why? I am using g++ compiler on Linux.
        I'm afraid the answer is "because the language was designed
        to not to support this". (That it's not impossible to do
        this can be seen in other languages.)
        Other languages are other languages. I'm not sure it would work
        that well in C++. (Amongst other things, it would offer yet
        another possibility for ambiguity in overload resolution. I
        hope you're not going to argue that overload resolution is too
        simple in C++, and needs to be made more complicated:-).)

        It's possible to do in C++, in specific cases where it makes
        sense. Just have the class return a proxy object with a

        template< typename T Proxy::operator T() const ;

        function.

        --
        James Kanze (GABI Software) email:james.kan ze@gmail.com
        Conseils en informatique orientée objet/
        Beratung in objektorientier ter Datenverarbeitu ng
        9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

        Comment

        • Hendrik Schober

          #5
          Re: return type deduction for a function template

          James Kanze wrote:
          On Oct 24, 9:08 am, Hendrik Schober <spamt...@gmx.d ewrote:
          >C++Liliput wrote:
          [...]
          >>Both the statements marked as "ERROR" throw errors during
          >>compilation . Now I can understand the first error in that a
          >>return type cannot be possibly deduced. But what I don't
          >>understand is the second error. Why is the return type not
          >>automatical ly deduced to be float? Such a deduction happens
          >>for arguments of a function template (in case we don't
          >>specify the data type during template instantiation
          >>directly). Then why cannot it happen for the return type?
          >>Is it a compiler bug or was it intended to be the behavior?
          >>If so why? I am using g++ compiler on Linux.
          >
          > I'm afraid the answer is "because the language was designed
          > to not to support this". (That it's not impossible to do
          > this can be seen in other languages.)
          >
          Other languages are other languages. I'm not sure it would work
          that well in C++. (Amongst other things, it would offer yet
          another possibility for ambiguity in overload resolution. I
          hope you're not going to argue that overload resolution is too
          simple in C++, and needs to be made more complicated:-).)
          I didn't say I want it. I was just rtying to prevent a
          useless discussion whether it is, in principle, possible
          to allow this in C++. Obviously, I failed... :)
          [...]
          Schobi

          Comment

          • Pete Becker

            #6
            Re: return type deduction for a function template

            On 2008-10-24 01:05:06 -0400, "C++Liliput " <aveekmisra@gma il.comsaid:
            Consider the following code
            >
            template<class T1, class T2>
            T2 sum(T1 a, T1 b)
            {
            T2 ret = a + b;
            return ret;
            }
            >
            int main()
            {
            float f1 = 2, f2 = 3;
            int i1 = 2, i2 = 4;
            >
            sum<float>(f1, f2); //ERROR
            float fret = sum<float>(f1, f2); //ERROR
            }
            >
            Both the statements marked as "ERROR" throw errors during compilation.
            Now I can understand the first error in that a return type cannot be
            possibly deduced. But what I don't understand is the second error. Why
            is the return type not automatically deduced to be float?
            Because the compiler doesn't look at how the return type is used to
            figure out what it should be. For example,

            void f(float);
            void f(double);
            f(sum<float>(f1 , f2));

            What should the return type of the template be here?

            --
            Pete
            Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
            Standard C++ Library Extensions: a Tutorial and Reference
            (www.petebecker.com/tr1book)

            Comment

            Working...