By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,012 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

function declaration returning array ref...

P: n/a
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

Oct 4 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"werasm" <w_*****@telkomsa.netwrites:
>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)
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
Oct 4 '06 #2

P: n/a
LR
werasm wrote:
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
Oct 4 '06 #3

P: n/a
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.

--

Frederick Gotham
Oct 4 '06 #4

P: n/a

LR wrote:
Try a search for "right left rule" at wwww.google.com or your favorite
search engine.
Thank you, that will be google (your response was helpfull). For now
I'll read this:

http://www.codeproject.com/cpp/complex_declarations.asp

BTW, are there any references in the standard as to how this "grammar"
works.

Kind regards,

Werner

Oct 5 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.