Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 16th, 2006, 10:16 PM   #1
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
Templates and Arrays

I recently came across this snippet of code:

cpp Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cstddef>
  3. #include <algorithm>
  4. #include <iterator>
  5.  
  6. template <typename T, size_t size>
  7. void print_all(const T (&arr)[size]) {
  8. std::copy(arr, arr + size, std::ostream_iterator<T>(std::cout, "\n"));
  9. }
  10.  
  11. int main() {
  12. int arr[] = { 1, 2, 3, 4 };
  13.  
  14. print_all(arr);
  15. }

I'm particularly curious as to why this section:

cpp Syntax (Toggle Plain Text)
  1. const T (&arr)[size]

works. I have done some searching, but to no avail. Any info, or links to info documenting this feature of C++ would be appreciated. Thanks
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Jul 17th, 2006, 4:21 AM   #2
Eoin
Hobbyist Programmer
 
Eoin's Avatar
 
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3 Eoin is on a distinguished road
This is a template in order to allow the function to work for all array types (well excluding dynamically created ones). If the function wasn't a template you would have to code one specifically for each array you'd use with it, for example;

cpp Syntax (Toggle Plain Text)
  1. void print_all(const int (&arr)[4]) {
  2. std::copy(arr, arr + 4, std::ostream_iterator<int>(std::cout, "\n"));
  3. }

The
cpp Syntax (Toggle Plain Text)
  1. const int (&arr)[4]
is just the way you write the type for a const reference to an int array containing 4 elements.
__________________
Visit my website BinaryNotions.
Eoin is offline   Reply With Quote
Old Jul 17th, 2006, 12:25 PM   #3
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 266
Rep Power: 4 Cache is on a distinguished road
I think the question is more concerned with the specifics of how the second template parameter is deduced.
Cache is offline   Reply With Quote
Old Jul 17th, 2006, 11:54 PM   #4
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 266
Rep Power: 4 Cache is on a distinguished road
Quote:
Originally Posted by Cache
I think the question is more concerned with the specifics of how the second template parameter is deduced.
Sorry to DP, but this has been bugging me, lol. The above quote should have read: I think the question is more concerned with the specifics of when (compile/link time vs runtime) the second template parameter is deduced.
Cache is offline   Reply With Quote
Old Jul 18th, 2006, 5:18 AM   #5
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
All template parameters are "deduced" at compile time.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:08 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC