问题
The following code is based on a snippet from here. I want a templated function that accepts a reference to an array of size that is deduced when the function is instantiated:
template<int size>
void myFunction( SomeType(¶m)[size] )
{
//use param and size here
}
//called like this:
SomeType array[SomeConstant];
myFunction( array ); //SomeConstant magically gets into the function as "size"
Now I'm confused with SomeType(¶m)[size]
. I'd expect the following to work:
template<int size>
void myFunction( (SomeType[size])& param ) {}
but it wouldn't compile.
Why do I need such weird syntax for "reference to array of fixed size"?
回答1:
The problem with your second declaration becomes what value should be used as the identifier? Should it be param
, or should it be SomeType
? Because of the parenthesis, the first part of the declaration would be parsed first, but if that's the case, and SomeType
was not the identifier, then at what point in the parsing does the identifier get named?
Using the clockwise parsing pattern of C/C++, where the identifier is the the inner-most unknown parsed element (i.e., something that isn't a known token or keyword ... see the link), syntax like
(SomeType[size])& param
would read "SomeType is an array of fixed 'size' that is a reference-type type for some object param
" which of course doesn't make any sense since the array has not been declared with the type of object that it is an array of. On the otherhand
SomeType(¶m)[size]
using the same parsing rules would read "param
is a reference to an array of fixed size that contains objects of type SomeType
". The latter is of course the declaration you want, and what makes sense to the C/C++ parser.
回答2:
It's based on the C declaration syntax. Of course, C doesn't have references, but it's meant to mimic the C syntax for pointers. Here's a pointer to an array (in C or C++):
int (*parray)[size];
The idea is that the declaration mimics the usage. It's meant to suggest that later on when you use parray
, the expression
(*parray)[0]
(or whatever index) is of type int. Of course, the symmetry breaks down for reference types, since the &
is used while declaring them but not referring to them.
回答3:
It's for consistency with C array declaration syntax.
With your proposed syntax, you'd also write:
int*[4] x;
instead of
int* x[4];
That's not a bad choice, but it's not what C uses, and C++ tries to be compatible.
回答4:
When declaring you should follow the rule:
Start form the name, go right when you can, go left when you must.
this rule is completely described in this useful link, and &
is handled as *
, this is the link:
http://unixwiz.net/techtips/reading-cdecl.html
In you delecration, you put []
before the name of the variable, which is illegal. The rule is that every *
and &
should come before the name, and every []
and ()
should come after the name.
来源:https://stackoverflow.com/questions/6456253/why-is-reference-to-array-defined-in-such-confusing-way-in-c