473,387 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 );
^
Feb 17 '06 #1
2 1659
* 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.
--
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?
Feb 17 '06 #2

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.

Feb 17 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Suzanne Vogel | last post by:
Is it possible to store a pointer to a template function? eg, template<class T> fooFunc() {...} fptr = fooFunc; // <-- I couldn't find any info here, not even in the forums:...
14
by: John Harrison | last post by:
Can anyone explain why this code fails to compile? #include <iostream> void func() { std::cout << "success!\n"; } struct S
11
by: BRG | last post by:
I know that default template arguments cannot be used in function templates but are default function parameters legal? That is, is this: ---------------------------------- #include <functional>...
3
by: Marcel Sebbao | last post by:
I want to use a function object for various callbacks, and also as a normal function. But this function is a template. It is fine, but then I need to specify all the time the template parameter...
5
by: Kyle | last post by:
I found this in my old junk testing path... /* get the size of the array */ template<typename T, int size> int getsz (T a) { return size;} Seems the compiler (gcc 3.4) can not compile the...
18
by: tbringley | last post by:
I am a c++ newbie, so please excuse the ignorance of this question. I am interested in a way of having a class call a general member function of another class. Specifically, I am trying to...
7
by: matthias.neubauer | last post by:
I have problems understanding how overloading of function templates works. Consider first the following code without any function templates ... int foo(const char *& c) { return 0; }
12
by: nooneinparticular314159 | last post by:
Hello. If I declare the following: template<int a, int b, int SomeArray> class DoSomething{ public: .. .. ..
5
by: C++Liliput | last post by:
Consider the following code template<class T1, class T2> T2 sum(T1 a, T1 b) { T2 ret = a + b; return ret; } int main()
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.