468,306 Members | 1,176 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,306 developers. It's quick & easy.

Reference to array, NOT array of references!

template<class T>
inline T& NthArrayMember( T & (array[]),size_t i)
{
return array[i - 1];
}

template<class T>
inline const T& NthArrayMember( const T & (array[]),size_t i)
{
return array[i - 1];
}
G++: declaration of `array' as array of references
I've tried about five different parenthesis combinations, but I just can't
get a reference to an array!

It's to be used as so:

int main()
{
int blah[50];

NthArrayMember(blah,45) = 56; //45th member = 56

extern void SomeFunc(int);

SomeFunc( NthArrayMember(blah,34) ); //Passes 34th member

}
-JKop
Jul 22 '05 #1
12 10212

Granted I get this to work, do you think the following
would be more efficent in that a temporary is unnecessary:

template<class T>
inline T& NthArrayMember( T & (array[]),size_t i)
{
return array[i -= 1]; //As opposed to just -
}
Jul 22 '05 #2

"JKop" <NU**@NULL.NULL> wrote in message
news:_E*****************@news.indigo.ie...
template<class T>
inline T& NthArrayMember( T & (array[]),size_t i)
{
return array[i - 1];
}

template<class T>
inline const T& NthArrayMember( const T & (array[]),size_t i)
{
return array[i - 1];
}
G++: declaration of `array' as array of references
I've tried about five different parenthesis combinations, but I just can't
get a reference to an array!

It's to be used as so:

int main()
{
int blah[50];

NthArrayMember(blah,45) = 56; //45th member = 56

extern void SomeFunc(int);

SomeFunc( NthArrayMember(blah,34) ); //Passes 34th member

}


Given that an array is not a type in the first place, I don't see how you
can create a reference to it. You can't even create a pointer to an array
(just a pointer to its first element). And, if the intent is to use it as
described, why not just:

blah[45] = 56;
SomeFunc(blah[34]);

?

-Howard



Jul 22 '05 #3
Howard posted:
why not just:

blah[45] = 56;
SomeFunc(blah[34]);

?


The sets the 46th member
That passes the 35th member.

Obviously I could write:

SomeFunc(blah[33]);

But I'm writing a pretty lengthly function and the computer and I think
differently: I associate the first member with 1, while the computer
associates it with 0. I'm bound to slip-up somewhere!

Anyway, here's the latest version:
template<class T, class R>
inline T& NthArrayMember( T (&array)[],R i)
{
return array[--i];
}

template<class T, class R>
inline const T& NthArrayMember( const T (& array)[],R i)
{
return array[--i];
}
g++ tester.cpp -ansi -pedantic -Wall -o t.exe

tester.cpp:83: parameter `array' includes reference to array of unknown
bound `
T[]'
tester.cpp:89: parameter `array' includes reference to array of unknown
bound `
const T[]'
tester.cpp: In function `int main()':
tester.cpp:99: `GetNthArrayMember' undeclared (first use this function)
tester.cpp:99: (Each undeclared identifier is reported only once for each
function it appears in.)
Any ideas?
-JKop
Jul 22 '05 #4
"JKop" <NU**@NULL.NULL> wrote...
[...]
template<class T, class R>
inline const T& NthArrayMember( const T (& array)[],R i)
{
return array[--i];
}


template<class T, int size, class R>
inline T& NthArrayMember(T (&array)[size], R i)
...

V
Jul 22 '05 #5
Okay, I've finally got it:
template<class T, class R>
inline T& NthArrayMember( T * const p_first_array_member,R i)
{
return p_first_array_member[--i];
}

template<class T, class R>
inline const T& NthArrayMember( const T * const p_first_array_member,R i)
{
return p_first_array_member[--i];
}
int main()
{
char blah[786];

extern void SomeFunc(char);

NthArrayMember(blah,56) = 72;

SomeFunc(NthArrayMember(blah,56));
}

void SomeFunc(char) { }

-JKop
Jul 22 '05 #6
Victor Bazarov posted:

template<class T, int size, class R>
inline T& NthArrayMember(T (&array)[size], R i)
...

V


I realized that finally but then decided that a pointer was
the way to go.

Thanks for the input.

-JKop
Jul 22 '05 #7
On Wed, 21 Jul 2004 15:23:30 -0700, Howard wrote:
Given that an array is not a type in the first place,
An array of a specific size is a type.
I don't see how
you can create a reference to it. You can't even create a pointer to an
array (just a pointer to its first element).


These are incorrect. You can have references and pointers to arrays
as well (not only to their first elements). The following program
demonstrates this:

#include <iostream>

size_t const arraySize = 10;

typedef int Array[arraySize];

// Function taking reference-to-array
void fill(Array & array, int value)
{
for (size_t i = 0; i != arraySize; ++i)
{
array[i] = value;
}
}

// Function taking const-reference-to-array
void printMiddle(Array const & array)
{
std::cout << array[arraySize / 2] << '\n';
}

int main()
{
Array one = { 0 };
fill(one, 1);
Array two = { 0 };
fill(two, 2);

// Pointer to array (not to the first element!)
Array * p = 0;

p = &one;

// Dereferencing pointer-to-array
printMiddle(*p);

p = &two;
printMiddle(*p);
}

Ali
Jul 22 '05 #8

"JKop" <NU**@NULL.NULL> wrote in message
news:m6*****************@news.indigo.ie...
Howard posted:
why not just:

blah[45] = 56;
SomeFunc(blah[34]);

?
The sets the 46th member
That passes the 35th member.

Obviously I could write:

SomeFunc(blah[33]);

But I'm writing a pretty lengthly function and the computer and I think
differently: I associate the first member with 1, while the computer
associates it with 0. I'm bound to slip-up somewhere!


Wow! That's pretty basic stuff. If you're using constants, why not declare
some constants and use those instead of integer literals as the array
locations?
Anyway, here's the latest version:
template<class T, class R>
inline T& NthArrayMember( T (&array)[],R i)
{
return array[--i];
}

template<class T, class R>
inline const T& NthArrayMember( const T (& array)[],R i)
{
return array[--i];
}
g++ tester.cpp -ansi -pedantic -Wall -o t.exe

tester.cpp:83: parameter `array' includes reference to array of unknown
bound `
T[]'
tester.cpp:89: parameter `array' includes reference to array of unknown
bound `
const T[]'
tester.cpp: In function `int main()':
tester.cpp:99: `GetNthArrayMember' undeclared (first use this function)
tester.cpp:99: (Each undeclared identifier is reported only once for each
function it appears in.)
Any ideas?
Yep. Since an array always decays to a pointer when passed as a parameter,
why not just use a pointer (T*) instead? You can still index it just like
you've shown.

(And why the use of class R for the index? Do you foresee using some other
kind of index?)


-JKop

Jul 22 '05 #9
Howard posted:
Wow! That's pretty basic stuff. If you're using constants, why not declare some constants and use those instead of integer literals as the array locations?
I have an array of digits:

wchar_t digits[] = L"1234567890";

To get 9:

NthArrayMember(digits, 9);

Now isn't that much more beautiful than specifying 8 when
you want 9! Again I must stress that this is a very
lengthly function!

(And don't suggest "0123456789"! ;-) )

(And why the use of class R for the index? Do you foresee using some other kind of index?)


You never know, it's possible to do

class SomeClass
{
operator[](SomeCrazyClass& blah);
}
There's no overhead in allowing for that, so I may aswell!
-JKop
Jul 22 '05 #10

"Ali Cehreli" <ac******@yahoo.com> wrote in message
news:pa**********************************@yahoo.co m...
On Wed, 21 Jul 2004 15:23:30 -0700, Howard wrote:
Given that an array is not a type in the first place,


An array of a specific size is a type.
I don't see how
you can create a reference to it. You can't even create a pointer to an
array (just a pointer to its first element).


These are incorrect. You can have references and pointers to arrays
as well (not only to their first elements). The following program
demonstrates this:

#include <iostream>

size_t const arraySize = 10;

typedef int Array[arraySize];

// Function taking reference-to-array
void fill(Array & array, int value)
{
for (size_t i = 0; i != arraySize; ++i)
{
array[i] = value;
}
}

// Function taking const-reference-to-array
void printMiddle(Array const & array)
{
std::cout << array[arraySize / 2] << '\n';
}

int main()
{
Array one = { 0 };
fill(one, 1);
Array two = { 0 };
fill(two, 2);

// Pointer to array (not to the first element!)
Array * p = 0;

p = &one;

// Dereferencing pointer-to-array
printMiddle(*p);

p = &two;
printMiddle(*p);
}

Ali


Well, now you've *created* a type (Array), which *can* be pointed to or
referenced. And the only reason you can do it at all is because you use a
compile-time constant to declare the array as a fixed size. That allows the
compiler to know how much memory the new type requires. This won't work for
a variable-length array passed as a parameter, because there is no type, and
you can't declare it as a type (because it's variable-length). (So, maybe I
should have specified that more clearly originally, but I thought it was
obvious in the context of the conversation my post was in.)

-Howard


Jul 22 '05 #11

I'd love to stay and chat, but it's 11:50pm here in Ireland
and I'm off to bed. Good night! (Well maybe some crap TV
first!)
-JKop
Jul 22 '05 #12

"JKop" <NU**@NULL.NULL> wrote in message
news:Xo*****************@news.indigo.ie...
Howard posted:
Wow! That's pretty basic stuff. If you're using

constants, why not
declare some constants and use those instead of integer

literals as the
array locations?


I have an array of digits:

wchar_t digits[] = L"1234567890";

To get 9:

NthArrayMember(digits, 9);

Now isn't that much more beautiful than specifying 8 when
you want 9! Again I must stress that this is a very
lengthly function!

(And don't suggest "0123456789"! ;-) )


Damn! You beat me to it! :-)

-Howard
Jul 22 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Andrew Fleet | last post: by
7 posts views Thread by Tim Clacy | last post: by
29 posts views Thread by shuisheng | last post: by
9 posts views Thread by Jack | last post: by
7 posts views Thread by arnuld | last post: by
31 posts views Thread by siddhu | last post: by
22 posts views Thread by Ruben | last post: by
275 posts views Thread by Astley Le Jasper | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.