visual-studio 为什么“数组”在 Visual-C++ 中被标记为保留字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/372580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:00:26  来源:igfitidea点击:

Why is "array" marked as a reserved word in Visual-C++?

arraysvisual-studiovisual-c++c++-cli

提问by CS student

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?

Visual Studio 语法高亮显示该词为蓝色,就好像它是关键字或保留词一样。我尝试在网上搜索它,但是“数组”这个词让搜索失败了,我得到的大部分页面都解释了什么是数组。这有什么用途?

回答by Judge Maygarden

It's not a reserved word under ISO standards. Microsoft's C++/CLIdefines arrayin 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 标准下的保留字。微软的C++/CLIcli 命名空间中定义了数组,Visual Studio 的语法高亮会将其视为保留字。这种用法将被视为供应商扩展,而不是任何国际 C 或 C++ 标准的一部分。

ISO C99Keywords:

ISO C99关键词:

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:

ISO C++98 关键词:

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

回答by Paul Tomblin

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

不是。至少不是在标准 C/C++ 中。

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

现在您可能会问为什么“条目”是 K&R 中 C 中的保留字,但在 C99 中不是 - 有人认为他们可能会在某个时候添加该功能,但最终决定反对。

回答by jeffm

It's used in C++/CLI.

它在 C++/CLI 中使用。

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

Visual C++ 语言参考:“使用数组关键字可以创建在公共语言运行时堆上分配的动态数组。”

回答by MSalters

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.

Visual Studio 从不费心为其漂亮的打印机定义不同的 C++ 语法。ISO C++、VC++、C++/CLI 或只是旧的 C - 都共享相同的语法。因此,像数组和接口这样的名称都被视为关键字。

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.

漂亮的打印机也很难发现 foo.cpp 中使用的 C++ 方言。您需要为此编译代码。目前漂亮的打印机可以对令牌进行操作,这意味着它只需要解析代码。

回答by warren

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

在什么版本?谷歌搜索“c++ 保留字”显示没有这样的用法。

I routinely use "array" in sample code.

我经常在示例代码中使用“数组”。

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

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

回答by sdaf

It is not a reserved word, but Microsoft Visual Studio decided to mark it blue as if it were a reserved word, but it most definitely is not according to "C++ Programming 5th Edition" by D.D. Malik.

它不是保留字,但 Microsoft Visual Studio 决定将其标记为蓝色,就好像它是保留字一样,但它绝对不是根据 DD Malik 的“C++ Programming 5th Edition”。

回答by phuclv

The fact that a word's being hightlighted in MSVC doesn't mean that it's a C/C++ keyword. As you can see, it also highlights many non-standard things like __int64, or even __int128although there's no 128-bit int type in VC.

在 MSVC 中突出显示某个词的事实并不意味着它是 C/C++ 关键字。如您所见,它还突出显示了许多非标准内容,例如__int64, 或者即使__int128VC 中没有 128 位 int 类型。