473,324 Members | 2,248 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,324 software developers and data experts.

template function issue

Hello everyone,
In the following code, how GetArrayLength(arr1) is matched to template
function size_t GetArrayLength(const T(&arr)[size])? My confusion is
how arr1 is matched to const T(&arr)[size]? I have tried to change
const T(&arr)[size] to const T(arr)[size]) but it does not work.

Expand|Select|Wrap|Line Numbers
  1. template<size_t size, typename T>
  2. size_t GetArrayLength(const T(&arr)[size])
  3. {
  4. return size;
  5. }
  6.  
  7. int main()
  8. {
  9. char arr1[] = "Hello World";
  10. std::cout << GetArrayLength(arr1) << std::endl;
  11.  
  12. return 0;
  13. }
  14.  

thanks in advance,
George
Dec 12 '07 #1
1 1020
George2 a écrit :
Hello everyone,
In the following code, how GetArrayLength(arr1) is matched to template
function size_t GetArrayLength(const T(&arr)[size])?
T(&arr)[size] is considered as a type. The same you could write a
specific typedef:

typedef int array_int[10];

The template matches the type <T,N"array of N elements of type T".
My confusion is
how arr1 is matched to const T(&arr)[size]? I have tried to change
const T(&arr)[size] to const T(arr)[size]) but it does not work.
For historical reasons arrays are no passed by values but are
implicitely cast into pointer so the following code doesn't pass by
value but takes a pointer:

void foo1(char array[12])
{
array[0]='G';
}

and the code is ok when arr1 used with:

void foo2(char array[42])
{
array[0]='G';
}

But not when using a type:

void foo3(char (&array)[42])
{
array[0]='G';
}
>
Expand|Select|Wrap|Line Numbers
  1. template<size_t size, typename T>
  2. size_t GetArrayLength(const T(&arr)[size])
  3. {
  4.     return size;
  5. }
  6. int main()
  7. {
  8.   char arr1[] = "Hello World";
  9.   std::cout << GetArrayLength(arr1) << std::endl;
Expand|Select|Wrap|Line Numbers
  1.  
  2. foo1(arr1);//arr1 is passed by reference
  3. std::cout << arr1  << std::endl;//prints "Gello ..."
  4. foo2(arr1);
  5. //following is error
  6. //foo3(arr1);
  7.         
  8.                 >
  9.   return 0;
  10. }
  11.  
  12.  
Dec 12 '07 #2

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

Similar topics

7
by: zhou | last post by:
Hi there, We have a compiler specific issue which requires us to force template instantiation. This works fine. The problem comes when I try using std:find() on vector. Since vector has no member...
12
by: Surya Kiran | last post by:
Hi all, I've written a function template. say template <class T> fn (T var) { ... } Is there any way, from within the function, can we check what type of argument we've passed on to the...
31
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will...
9
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of different objects of different type (they are all the...
5
by: Lionel B | last post by:
Greetings, I am trying to implement "element-wise" arithmetic operators for a class along the following lines (this is a simplified example): // ----- BEGIN CODE ----- struct X { int a,b;
5
by: Eric Lilja | last post by:
Ok, this code doesn't compile: #include <iostream> #include <ostream> /* Just for you, Mike :-) */ template<typename T> class Couple { public: Couple(const T& ax, const T& ay) : x(ax), y(ay)...
7
by: mathieu | last post by:
Hello, I did read the FAQ on template(*), since I could not find an answer to my current issue I am posting here. I have tried to summarize my issue in the following code (**). Basically I am...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
6
by: bogus1one | last post by:
Hi All Given the following: // NamespaceTemplate.cpp : Defines the entry point for the console application. // #include "stdafx.h"
6
by: Lawrence Spector | last post by:
I ran into a problem using g++. Visual Studio 2005 never complained about this, but with g++ I ran into this error. I can't figure out if I've done something wrong or if this is a compiler bug. ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.