5

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:

enter image description here

Expected output:

enter image description here

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.)

5

(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:

enter image description here

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:

enter image description here

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:

enter image description here

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!)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.