function template arg deduction with arrays

K

Kyle

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 );
^
 
A

Alf P. Steinbach

* Kyle:
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 );
^

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

Maxim Yegorushkin

Alf P. Steinbach wrote:

[]
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 );
^

I believe this is a bug in Comeau. The code

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top