function template arg deduction with arrays

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

    #1

    function template arg deduction with arrays

    I used following template function:

    template<class Elem, int Size>
    int tabSize( Elem (&tab)[Size] ) {
    return Size;
    }

    tu deduce size and type of an array passed as parameter, but someone
    proposed making the argument const (perhaps just out const correctnes),
    and it become:

    template<class Elem, int Size>
    int tabSize( const Elem (&tab)[Size] ) {
    return Size;
    }

    now, Comeau online compiler refuses to accept following code

    int main() {
    int tab[]={1,2,3,4,5};
    tabSize( tab );
    }

    and this rises a question - why, is there some special rule regarding
    arrays with function templates ??? (or perhaps comeau is wrong)

    i managed to get rid of nontype template parameter and still get the
    same error

    template<class Elem>
    void foo( const Elem (&tab)[5] ) {}

    int main() {
    int tab[5] = {1,2,3,4,5};
    foo( tab );
    }

    "ComeauTest .c", line 6: error: no instance of function template "foo"
    matches the
    argument list
    The argument types that you used are: (int [5])
    foo( tab );
    ^
  • Alf P. Steinbach

    #2
    Re: function template arg deduction with arrays

    * Kyle:[color=blue]
    > I used following template function:
    >
    > template<class Elem, int Size>
    > int tabSize( Elem (&tab)[Size] ) {
    > return Size;
    > }
    >
    > tu deduce size and type of an array passed as parameter, but someone
    > proposed making the argument const (perhaps just out const correctnes),
    > and it become:
    >
    > template<class Elem, int Size>
    > int tabSize( const Elem (&tab)[Size] ) {
    > return Size;
    > }
    >
    > now, Comeau online compiler refuses to accept following code
    >
    > int main() {
    > int tab[]={1,2,3,4,5};
    > tabSize( tab );
    > }
    >
    > and this rises a question - why, is there some special rule regarding
    > arrays with function templates ??? (or perhaps comeau is wrong)
    >
    > i managed to get rid of nontype template parameter and still get the
    > same error
    >
    > template<class Elem>
    > void foo( const Elem (&tab)[5] ) {}
    >
    > int main() {
    > int tab[5] = {1,2,3,4,5};
    > foo( tab );
    > }
    >
    > "ComeauTest .c", line 6: error: no instance of function template "foo"
    > matches the
    > argument list
    > The argument types that you used are: (int [5])
    > foo( tab );
    > ^[/color]

    I believe this is a bug in Comeau. The code

    template<class Elem, int Size>
    int tabSize( const Elem (&tab)[Size] ) {
    return Size;
    }

    int main()
    {
    int tab[]={1,2,3,4,5};
    tabSize( tab );
    }

    compiles fine with MSVC 7.1 and with g++ 3.4.4.


    --
    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

    • Maxim Yegorushkin

      #3
      Re: function template arg deduction with arrays


      Alf P. Steinbach wrote:

      []
      [color=blue][color=green]
      > > template<class Elem>
      > > void foo( const Elem (&tab)[5] ) {}
      > >
      > > int main() {
      > > int tab[5] = {1,2,3,4,5};
      > > foo( tab );
      > > }
      > >
      > > "ComeauTest .c", line 6: error: no instance of function template "foo"
      > > matches the
      > > argument list
      > > The argument types that you used are: (int [5])
      > > foo( tab );
      > > ^[/color]
      >
      > I believe this is a bug in Comeau. The code[/color]

      So do I, because it does compile this code:

      int a[2];
      int const(&b)[2] = a;

      IOW, it does bind a reference to an array of const to an array of
      non-const.

      Comment

      Working...