function template

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

    #1

    function template

    I found this in my old junk testing path...

    /* get the size of the array */
    template<typena me T, int size>
    int getsz (T a[size]) { return size;}

    Seems the compiler (gcc 3.4) can not compile the code when the template
    is used, it can not deduct the parameter.

    Was this code supposed to work? If not, which template parameter
    deduction rule does it violate?

    thanks!

  • Rolf Magnus

    #2
    Re: function template

    Kyle wrote:
    [color=blue]
    > I found this in my old junk testing path...
    >
    > /* get the size of the array */
    > template<typena me T, int size>
    > int getsz (T a[size]) { return size;}
    >
    > Seems the compiler (gcc 3.4) can not compile the code when the template
    > is used, it can not deduct the parameter.
    >
    > Was this code supposed to work?
    > If not, which template parameter
    > deduction rule does it violate?[/color]

    I'm not sure, but I think it's due to the fact that in a parameter,
    'T a[size]' is actually the syntax for a pointer to T. 'size' is ignored.
    So I guess that - when called - the argument is converted to T*, and so the
    compiler has no way to figure out the size template argument.
    After all, you could also call it as:

    char* p;
    getsz(p);

    What would 'size' now be?


    Comment

    • Neelesh Bodas

      #3
      Re: function template


      Kyle wrote:[color=blue]
      > I found this in my old junk testing path...
      >
      > /* get the size of the array */
      > template<typena me T, int size>
      > int getsz (T a[size]) { return size;}
      >
      > Seems the compiler (gcc 3.4) can not compile the code when the template
      > is used, it can not deduct the parameter.
      >
      > Was this code supposed to work? If not, which template parameter
      > deduction rule does it violate?
      >[/color]
      when array is passed as a reference, the size is also passed.

      int getsz (T (&a) [size]) { return size;}

      this would work.

      Hope this helps.


      [color=blue]
      > thanks![/color]

      Comment

      • Victor Bazarov

        #4
        Re: function template

        Kyle wrote:[color=blue]
        > I found this in my old junk testing path...
        >
        > /* get the size of the array */
        > template<typena me T, int size>
        > int getsz (T a[size]) { return size;}
        >
        > Seems the compiler (gcc 3.4) can not compile the code when the template
        > is used, it can not deduct the parameter.[/color]

        "cannot deduce", not "deduct".
        [color=blue]
        > Was this code supposed to work? If not, which template parameter
        > deduction rule does it violate?[/color]

        It is impossible to deduce that is the arrays size from a pointer. You
        need a reference to an array:

        template<typena me T, int size>
        int getsz(T (&a)[size]) { return size; }

        Of course, this will only compile if the argument you're passing is in
        fact an array, not a pointer. But if it's so, you already know the
        size of it at the point where you call that function (all arrays have
        static dimensions). So, it seems that while such function is interesting
        as a template exercise, I can't find a good use for it.

        V

        Comment

        • Kyle

          #5
          Re: function template

          Guys, Thanks for the kind reply and sorry for my bad English.

          Comment

          • Victor Bazarov

            #6
            Re: function template

            Kyle wrote:[color=blue]
            > Guys, Thanks for the kind reply and sorry for my bad English.[/color]

            Don't worry about your English. "Deduce" and "deduct" have the common
            root, 'ducere', Latin for "lead" [out of]. The difference is subtle (if
            any at all). In modern English "deduct" chiefly means "remove, subtract"
            (as in "expenses from being taxed", for example), whereas "deduce" means
            "to come to a logical conclusion by means of deduction", mainly. Both
            involve elimination of some kind. Besides, "deduct" does have the second
            meaning "infer, deduce". I only corrected you because the Standard uses
            the term "deduce" for figuring out the template arguments.

            V

            Comment

            Working...