function declaration returning array ref...

W

werasm

Hi all,

I have recently come accross this function declaration syntax.
Initially it was puzzling (still is, to be honest), but I now realize
that it declares a function that returns a reference to an array.

The declarator:

char (& foo() )[6]; //(1)

I would have suspected something like...

char(&)[6] foo(); //(2)

.... to be the declarator for a function returning a ref to an array,
but it does not seem so. Could someone explain this syntax to me, and
maybe a reason why (2) is not the function declarator syntax, and (1)
is so different from other function declarators.

Regards,

Werner
 
T

Tim Love

werasm said:
I have recently come accross this function declaration syntax.
Initially it was puzzling (still is, to be honest), but I now realize
that it declares a function that returns a reference to an array.
The declarator:
char (& foo() )[6]; //(1)

A free program called c++decl (aka cdecl) does a fair job of decoding such
stuff
tpl: c++decl explain "char (& foo() )[6]"
declare foo as function returning reference to array 6 of char
By experimenting with it, you can learn quite a lot. Alternatively, find
a book (maybe even a C one) which explains how to unravel these things by
working from the middle
 
L

LR

werasm said:
Hi all,

I have recently come accross this function declaration syntax.
Initially it was puzzling (still is, to be honest), but I now realize
that it declares a function that returns a reference to an array.

The declarator:

char (& foo() )[6]; //(1)

I would have suspected something like...

char(&)[6] foo(); //(2)

... to be the declarator for a function returning a ref to an array,
but it does not seem so. Could someone explain this syntax to me, and
maybe a reason why (2) is not the function declarator syntax, and (1)
is so different from other function declarators.

Try a search for "right left rule" at wwww.google.com or your favorite
search engine.

LR
 
F

Frederick Gotham

werasm posted:
The declarator:

char (& foo() )[6]; //(1)

I would have suspected something like...

char(&)[6] foo(); //(2)

... to be the declarator for a function returning a ref to an array,
but it does not seem so. Could someone explain this syntax to me, and
maybe a reason why (2) is not the function declarator syntax, and (1)
is so different from other function declarators.


That's just the grammar of declarations in C++ (and C aswell). You start
off with the "raw type", which may or may not have const or volatile:

int
int const
int volatile
int const volatile

From there, you give it a name:

int const volatile i

If you want to alter it any further, all alterations must orbit around the
name, without touching the "raw type". For instance, let's turn it into an
array:

int const volatile i[5]

Now, if we want to turn it into a pointer to an array, we must put the
asterisk with the name:

int const volatile (*i)[5];

Or a const pointer:

int const volatile (*const i)[5];

If you like your own syntax though, then maybe go with:

template<class T>
struct TypeProcurer {
typedef T Type;
};

TypeProcurer<int(&)[6]>::Type Func(int)
{

}

However I'd suggest you simply learn the grammar rather than resort to
these novice measures.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top