C + + 14是否向 C + + 添加了新的关键字?

C + + 标准委员会倾向于避免在语言中添加新的关键字,但是 C + + 11却不是这样。一些例子:

constexpr
decltype
thread_local
auto // New usage
noexcept
nullptr
static_assert
alignof
alignas

C + + 14是否引入了新的关键字?

11926 次浏览

Table 4 (Keywords) in N3936 (C++14):

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

Table 4 in N3337 (C++11):

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

...which is a long-winded way of saying "no".

(override and final are "identifiers with special meaning" and are listed in Table 3; and etc. are "alternative representations...for certain operators and punctuators" and are listed in Table 5. Neither table changed between C++11 and C++14.)

I'm posting this answer for the sake of giving tools for finding answers to similar questions.

The standard draft is currently kept in a public GitHub repository. That means you can ask this question to GitHub itself!

The keywords table is on the file source/lex.tex. If you do a blame on it, we can find that the last change to the keywords table took place back in August 2011 (it's actually the first commit: that table hasn't changed since the repo went live around the time C++11 was being finalised).

Alternatively we can ask GitHub to compare the two drafts that were sent for ballot for both versions of the standard: N3337 and N3936. A diff between those two shows that the changes to lex.tex did not change anything in the keywords table.

No new keywords will be added with C++14. This is unsurprising as C++14 is intended as a small upgrade to C++11 mainly involved in cleaning up bugs and making small, low impact, improvements. The next major change is likely to be C++'17' where I would expect new keywords once more.

The C++ Standards Committee tends to shy away from adding new keywords to the language, yet with C++11 that was not the case.

I think it's worth considering why the committee shies away from adding new keywords (and co-incidentally why you are wrong to include auto on your list). The main problem with new keywords is that in C++ you can't use a keyword as an identifier which means that adding a new keyword breaks existing code. Repurposing auto, then, doesn't break their rule because no existing code could use auto as an identifier anyway.

So in order to accept a new keyword there needs to be a justification that outweighs the cost of a potential clash with existing code and no sensible way to implement the same thing without a new keyword. In the case of C++11, the committee accepted a few proposals that required new keywords since they felt that that the benefit outweighed the cost not because they don't hate to add new keywords.

It's also why, if you look down the list you gave, each one is a compound keyword since that reduces the chance that they'll clash with existing identifiers.