views:

2727

answers:

9

Visual Studio syntax highlighting colors this word blue as if it were a keyword or reserved word. I tried searching online for it but the word "array" throws the search off, I get mostly pages explaining what an array is. What is it used for?

+8  A: 

It isn't. At least not in standard C/C++.

Now you might well ask the reason "entry" was a reserved word in C in K&R but not in C99 - somebody thought they might add the feature at some point, but eventually decided against it.

Paul Tomblin
"entry" is not a reserved word
warren
It was in C up until C99.
Paul Tomblin
but isn't now :)
warren
Nice edit, EvilTeach.
Paul Tomblin
+38  A: 

It's not a reserved word under ISO standards. Microsoft's C++/CLI defines array in the cli namespace, and Visual Studio's syntax highlighting will treat it as a reserved word. This usage would be considered a vendor extension and not a part of any international C or C++ standard.

ISO C99 Keywords:

auto        enum        restrict    unsigned
break       extern      return      void
case        float       short       volatile
char        for         signed      while
const       goto        sizeof      _Bool
continue    if          static      _Complex
default     inline      struct      _Imaginary
do          int         switch
double      long        typedef
else        register    union

ISO C++98 Keywords:

and         double          not                 this 
and_eq      dynamic_cast    not_eq              throw 
asm         else            operator            true 
auto        enum            or                  try 
bitand      explicit        or_eq               typedef 
bitor       export          private             typeid 
bool        extern          protected           typename 
break       false           public              union 
case        float           register            unsigned 
catch       for             reinterpret_cast    using 
char        friend          return              virtual 
class       goto            short               void 
compl       if              signed              volatile 
const       inline          sizeof              wchar_t 
const_cast  int             static              while 
continue    long            static_cast         xor 
default     mutable         struct              xor_eq
delete      namespace       switch     
do          new             template
Judge Maygarden
+1  A: 

In what edition? A Google search for "c++ reserved words" shows no such usage.

I routinely use "array" in sample code.

http://cs.smu.ca/~porter/csc/ref/cpp_keywords.html

warren
A: 

It isn't?

What source identifies "array" (or "entry") as key words in C++? A quick Google search gives several lists of C++ reserved words; non list "array" or "entry". G++ doesn't seem to recognize them either.

ejgottl
"entry" is reserved in the C standard, not in the C++ standard.
Paul Tomblin
"entry" is not reserved in the C99 standard; it's possible it was reserved in C90, though
Adam Rosenfield
The question mark after the first sentence gives the wrong impression - that you believe that 'array' is a key word and "what do you mean by saying it isn't a keyword".
Jonathan Leffler
entry was reserved by the C89 standard - but had no use.
Jonathan Leffler
A: 

VS 2005 colors it blue as if it was reserved. Microsoft couldn't be wrong, could they? :)

Steve Fallows
Did Microsoft write the international standard? Visual Studio colors lots of words blue that aren't keywords, including 'entry', 'interface', etc.
Adam Rosenfield
Yes this is exactly why I'm asking. Any idea why? I mean, they probably did decide to color it blue for some reason, even if it's a deviation from the C99 standard. I'm trying to decide whether to count its use as a variable name as bad programming in a code evaluation exercise.
CS student
Well, VC++ will also highlight other C++/CLI keywords such as "where". Doesn't mean you shouldn't use them. They're all contextual keywords, so even in VC++ your use of them as identifiers won't ever conflict with CLI extensions.
Pavel Minaev
+7  A: 

Apparently it's used in C++/CLI.

Visual C++ Language Reference: "The array keyword lets you create a dynamic array that is allocated on the common language runtime heap."

jeffm
A: 

Does Visual Studio highlight class names from the standard library, like std::vector, in a similar way? If yes, then array is defined there as well I guess

Later: yes, it seems std::array is a proposed addition to the 0x standard

dmityugov
No, VS doesn't. `array` is only highlighted because it's a C++/CLI keyword, as others have pointed out.
Konrad Rudolph
+1  A: 

Visual Studio never bothered with defining different C++ grammars for their pretty printer. ISO C++, VC++, C++/CLI, or just old C - all share the same grammar. So, names like array and interface are all treated as if they were keywords.

It would also be quite hard for the pretty printer to spot the C++ dialect used in foo.cpp. You'd need to compile the code for that. Currently the pretty printer can operate on tokens, which means it only needs to parse the code.

MSalters
On the other hand, it already has more information than just simple parsing, i.e Intellisense. I guess MS never thought it was worth the effort.
Skurmedel
A: 

I am also having a problem with either a problem in my head or an inconsistency in C++ reserved keywords list (or, maybe a compiler (gcc 3.4.4 on cygwin, launched by g++ -Wall) bug?).

Here's a useless program which does not compile on my gcc 3.4.4:

#include <iostream>
using namespace std;
struct link {
    link* next;
};
void smth(link* L) {
}
int main() {
    return 0;
}

Compiler issues the following error messages:

.cpp:6: error: variable or field 'smth' declared void
.cpp:6: error: 'L' was not declared in this scope
.cpp:6: error: expected ',' or ';' before '{' token

Clearly, none of these errors makes sense. So, after going crazy for about twenty minutes, I changed "link" to "node":

#include <iostream>
using namespace std;
struct node {
    node* next;
};
void smth(node* L) {
}
int main() {
    return 0;
}

And voila! the program compiles. "link" is not a reserved word according to the lists I could google.

Is it only me not knowing C++ or is it also (not excluding the first) something wrong with the compiler?

zilupe
That's why namespaces were born. See std::list : http://www.sgi.com/tech/stl/List.html
strager
Er, ignore my comment... I misread "link" as "list". Still, use std:: instead of the using statement and your problem is solved.
strager
If you mean "remove line `using namespace std;'" and use std:: when i need it, then it's not fixing the problem.
zilupe
Cygwin is a Unix-like system. "link" is a global function declared in unistd.h, which iostream ended up including. This is what is confusing the compiler; it is probably using the definition of "link" as a function, instead of your definition as a struct. When compiling for Unix, you should avoid using names reserved for the POSIX headers; when compiling for Win32, you should avoid the names from the Win32 headers. Since you are using cygwin, you have to avoid both sets of names.
CesarB