Pretty straightforward: How to highlight constexpr in cc-mode (editing C++ code)?


If I use const as in the following example

const double = 3.14

const gets highlighted as a keyword(1) (i.e., in pink), while if I write

constexpr double = 3.14

constexpr gets highlighted as all-faces (i.e., no highlighting at all).

Ideally, a simple solution like writing

(add-to-keywords "constexpr") ;; pseudo-code

in my .emacs without tinkering with the code of cc-mode would be the best for me.


This question might be related: Change the way CC-mode font-lock the C++11 auto syntax.


(1) I think that it is a keyword because placing the pointer on the word and doing M-x customize-face RET I get: Customize face (default ``font-lock-keyword-face'): and pressing RET I get redirected to the “Easy Customization” wizard.


1 Answer 1


The incantation you want is:

(font-lock-add-keywords 'c++-mode
                        '(("constexpr" . 'font-lock-keyword-face)))
  • 1
    It's dotted pair syntax for a cons cell. If you leave out the "." the keywords get interpreted differently. See the help for font-lock-keywords for what a "non-dotted" list would mean.
    – erikstokes
    Commented Mar 2, 2015 at 23:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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