Connecting Tech Pros Worldwide Help | Site Map

Reference to array, NOT array of references!

JKop
Guest
 
Posts: n/a
#1: Jul 22 '05
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
JKop
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Reference to array, NOT array of references!



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 -
}
Howard
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Reference to array, NOT array of references!



"JKop" <NULL@NULL.NULL> wrote in message
news:_EBLc.5178$Z14.6424@news.indigo.ie...[color=blue]
> 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
>
> }[/color]

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







JKop
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Reference to array, NOT array of references!


Howard posted:
[color=blue]
> why not just:
>
> blah[45] = 56;
> SomeFunc(blah[34]);
>
> ?[/color]

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
Victor Bazarov
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Reference to array, NOT array of references!


"JKop" <NULL@NULL.NULL> wrote...[color=blue]
> [...]
> template<class T, class R>
> inline const T& NthArrayMember( const T (& array)[],R i)
> {
> return array[--i];
> }[/color]

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

V


JKop
Guest
 
Posts: n/a
#6: Jul 22 '05

re: Reference to array, NOT array of references!


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
JKop
Guest
 
Posts: n/a
#7: Jul 22 '05

re: Reference to array, NOT array of references!


Victor Bazarov posted:

[color=blue]
> template<class T, int size, class R>
> inline T& NthArrayMember(T (&array)[size], R i)
> ...
>
> V[/color]

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

Thanks for the input.

-JKop
Ali Cehreli
Guest
 
Posts: n/a
#8: Jul 22 '05

re: Reference to array, NOT array of references!


On Wed, 21 Jul 2004 15:23:30 -0700, Howard wrote:
[color=blue]
> Given that an array is not a type in the first place,[/color]

An array of a specific size is a type.
[color=blue]
> 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).[/color]

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
Howard
Guest
 
Posts: n/a
#9: Jul 22 '05

re: Reference to array, NOT array of references!



"JKop" <NULL@NULL.NULL> wrote in message
news:m6CLc.5184$Z14.6454@news.indigo.ie...[color=blue]
> Howard posted:
>[color=green]
> > why not just:
> >
> > blah[45] = 56;
> > SomeFunc(blah[34]);
> >
> > ?[/color]
>
> 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!
>[/color]

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?
[color=blue]
> 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?[/color]

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?)
[color=blue]
>
>
> -JKop[/color]


JKop
Guest
 
Posts: n/a
#10: Jul 22 '05

re: Reference to array, NOT array of references!


Howard posted:
[color=blue]
> Wow! That's pretty basic stuff. If you're using[/color]
constants, why not[color=blue]
> declare some constants and use those instead of integer[/color]
literals as the[color=blue]
> array locations?[/color]

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"! ;-) )

[color=blue]
> (And why the use of class R for the index? Do you[/color]
foresee using some[color=blue]
> other kind of index?)[/color]

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
Howard
Guest
 
Posts: n/a
#11: Jul 22 '05

re: Reference to array, NOT array of references!



"Ali Cehreli" <acehreli@yahoo.com> wrote in message
news:pan.2004.07.21.15.41.08.653476.18901@yahoo.co m...[color=blue]
> On Wed, 21 Jul 2004 15:23:30 -0700, Howard wrote:
>[color=green]
> > Given that an array is not a type in the first place,[/color]
>
> An array of a specific size is a type.
>[color=green]
> > 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).[/color]
>
> 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[/color]

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




JKop
Guest
 
Posts: n/a
#12: Jul 22 '05

re: Reference to array, NOT array of references!



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
Howard
Guest
 
Posts: n/a
#13: Jul 22 '05

re: Reference to array, NOT array of references!



"JKop" <NULL@NULL.NULL> wrote in message
news:XoCLc.5190$Z14.6539@news.indigo.ie...[color=blue]
> Howard posted:
>[color=green]
> > Wow! That's pretty basic stuff. If you're using[/color]
> constants, why not[color=green]
> > declare some constants and use those instead of integer[/color]
> literals as the[color=green]
> > array locations?[/color]
>
> 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"! ;-) )[/color]

Damn! You beat me to it! :-)

-Howard


Closed Thread


Similar C / C++ bytes