# Highlight new C++ keywords introduced in C++11, C++17, and C++20 in the listings package

C++11 added a bunch of new keywords: alignas, alignof, constexpr, etc. But they don't seem to be supported by the listings package.

\documentclass{article}

\usepackage{listings}
\usepackage{xcolor}

\lstset{
language = [11]C++,
basicstyle = \ttfamily,
keywordstyle = \color{blue},
columns = flexible
}

\begin{document}

\begin{lstlisting}
constexpr int i = 0;
\end{lstlisting}

\end{document}


The constexpr is not recognized as a keyword:

Expected output:

How can I enable the C++11 keywords? How about C++20 keywords like requires, concept, consteval, etc.?

(Note: This is a self-answer question. I know this question can be closed as a duplicate of many others, such as Extend a language with additional keywords?, but they do not specifically address new C++ keywords. I am frequently asked about this, and I also need this very often, so I decided to write it down for future reference.)

(Reference: https://en.cppreference.com/w/cpp/keyword)

# C++11 keywords

The listings package has built in support for C++11. Use language = [11]C++ option to alignas (no pun intended) C++11:

\documentclass{article}

\usepackage{listings}
\usepackage{xcolor}

\lstset{
language = [11]C++,
basicstyle = \ttfamily,
keywordstyle = \color{blue},
columns = flexible
}

\begin{document}

\begin{lstlisting}
constexpr int i = 0;
\end{lstlisting}

\end{document}


This yields the desired result:

# C++11 identifiers with special meaning

## Treated as keywords

C++11 introduces two identifiers with special meaning: override and final. By default, they are not highlighted. You can add them manually to the keyword list:

\lstset{
morekeywords = {final, override}
}


Minimal example:

\documentclass{article}

\usepackage{listings}
\usepackage{xcolor}

\lstset{
language = [11]C++,
basicstyle = \ttfamily,
keywordstyle = \color{blue},
columns = flexible
}

\lstset{
morekeywords = {final, override}
}

\begin{document}

\begin{lstlisting}
override
final
\end{lstlisting}

\end{document}


This yields:

## Treated specially

If you want differentiate between keywords and identifiers with special meaning, you can override (no pun intended) this by introducing a special emphasis class for them:

\lstset{
emph = {[11]final, override},
emphstyle = \color{green}
}


Minimal example:

\documentclass{article}

\usepackage{listings}
\usepackage{xcolor}

\lstset{
language = [11]C++,
basicstyle = \ttfamily,
keywordstyle = \color{blue},
columns = flexible
}

\lstset{
emph = {[11]final, override},
emphstyle = \color{green}
}

\begin{document}

\begin{lstlisting}
constexpr
override
final
\end{lstlisting}

\end{document}


This yields the final (no pun intended) result:

# Other

For future reference, here I give the list of C++20 keywords, C++20 identifiers with special meaning, etc. If the maintainer of the listings package sees this, please add the relevant part to language = [20]C++!

## C++20 keywords

\lstset{
morekeywords = {
char8_t,
concept,
co_await,
co_return,
co_yield,
requires
}
}


## C++20 identifiers with special meaning

\lstset{
emph = {[20]
import,
module
}
}


(Note: the original version included audit and axiom. The Contract TS was later removed from C++20, so audit and axiom are no longer identifiers with special meaning in C++20.)

## Transactional Memory Technical Specification (TM TS) keywords

\lstset{
morekeywords = {
atomic_cancel,
atomic_commit,
atomic_noexcept,
synchronized
}
}


## Transactional Memory Technical Specification (TM TS) identifiers with special meaning

\lstset{
emph = {[42]
transaction_safe,
transaction_dynamic
}
}


## Reflection TS

\lstset{
morekeywords = {
reflexpr
}
}


(Note: all numbers were chosen arbitrarily. Don't forget to adjust them to avoid name clash with existing identifier classes!)