0

While using "array" as an identifier Codeblocks highlighted it like the other keywords. I searched it up in Why is "array" marked as a reserved word in Visual-C++?

But the answers were outdated. If yes, how is it used ?

Community
  • 1
  • 1
Tasdik Rahman
  • 2,160
  • 1
  • 25
  • 37
  • 4
    No, it isn't. There's a plain table of keywords in the standard: http://i.imgur.com/N5RfKh2.png. By the way, you can change which words C::B highlights in the settings. – chris Aug 15 '14 at 12:35
  • 4
    No, see e.g. [this up to date reference](http://en.cppreference.com/w/cpp/keyword). However, since the C++11 standard it is a [standard container](http://en.cppreference.com/w/cpp/container/array). – Some programmer dude Aug 15 '14 at 12:37
  • That's because "array" is in your editor's list of keywords, either because someone thought it was a good idea to colour it like one, or because the list is shared with a language where "array" *is* a keyword. You can probably edit that list if you want a standards-compliant one. – molbdnilo Aug 15 '14 at 13:57
  • CodeBlocks would probably also highlight a variable called `string` - usually syntax highlighters look for specific words and don't care about the context so there is no way for the syntax highlighter to know if you had earlier `#include` and `using namespace std;` or not and so it is being cautious. – Jerry Jeremiah Feb 19 '21 at 00:12

4 Answers4

4

array is not a keyword, but the C++11 standard defines its STL with a std::array template container. You should prefer

 std::array<int,5> tab;

instead of int tab[5]; because std::array have interesting functions and works better with other parts of the STL library.

Since it is a standard container, I would advise you to avoid using the array (or vector, because of std::vector, etc...) identifier in your own code (especially in reusable headers), to avoid future potential conflicts with <array> header, and also for readability reasons. But in principle you could define your own array but I don't recommend that.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
4

No, array is not a keyword.

Still, there is a C++11 standard-library type: std::array, a fixed-length array container.

Here a list of the keywords from the C++1y draft:

alignas    continue     friend    register         true
alignof    decltype     goto      reinterpret_cast try
asm        default      if        return           typedef
auto       delete       inline    short            typeid
bool       do           int       signed           typename
break      double       long      sizeof           union
case       dynamic_cast mutable   static           unsigned
catch      else         namespace static_assert    using
char       enum         new       static_cast      virtual
char16_t   explicit     noexcept  struct           void
char32_t   export       nullptr   switch           volatile
class      extern       operator  template         wchar_t
const      false        private   this             while
constexpr  float        protected thread_local
const_cast for          public    throw

These alternative representations (whose very existence I dislike, but that's just me) are not keywords though still reserved:

and    and_eq bitand bitor compl  not
not_eq or     or_eq  xor   xor_eq

Contextual keywords (override control, put at the end of the function declaration in a class (new not listed because already a keyword))

final   override
Pokechu22
  • 4,984
  • 9
  • 37
  • 62
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • what about `size_t`? I find that code using `size_t` (instead of the correct `std::size_t`) tends to compile ... – Walter Aug 15 '14 at 15:35
  • @Walter: Any standard header may include any other, as the creator saw fit, and the C inherited and derived headers can contain `::size_t`, which in that case must be equal to `std::size_t` and `decltype(sizeof 0)`. – Deduplicator Aug 15 '14 at 15:56
  • Is such a header still standard compliant? It pollutes the global namespace which I thought shouldn't happen. – Walter Aug 16 '14 at 18:00
3

array is a standard container, as you can see here.

It doesn't belong to the keywords, but it's part of the standard library.

Christophe
  • 68,716
  • 7
  • 72
  • 138
3

C++ Keywords are listed in the C++ standard, section § 2.13.

array is not listed there, so it isn't.


Note:

  • std::array is a standard type, but not a keyword.
  • array is a valid identifier, but it certainly is discouraged because of the previous point.
quantdev
  • 23,517
  • 5
  • 55
  • 88