Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 518603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:57:14+00:00 2026-05-13T07:57:14+00:00

I am using the standard (shipped) Emacs C++ mode but I have a slight

  • 0

I am using the standard (shipped) Emacs C++ mode but I have a slight itch that I am looking to get scratched. How would I go about properly highlighting the types inside of a sizeof and the casts in C++?

For example:

  1. A Cast

    Type * pointer = reinterpret_cast <Type *> (original);

  2. Sizeof

    std::cout << sizeof (Type) << "\n";

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:57:14+00:00Added an answer on May 13, 2026 at 7:57 am

    Add these expressions to your .emacs, or evaluate them with M-:.

    Sizeof (that’s the easier of the two cases)

    The regex highlights any combination (indicated by the bracket expression [...]; regarding syntax, please see the note below) of alphanumeric, whitespace and asterisk within parentheses and preceded by sizeof.

    (font-lock-add-keywords 'c++-mode
      '(("\\<sizeof[[:space:]]*(\\([[:alnum:][:space:]*]+\\))"
      1 font-lock-type-face t)))
    

    The number 1 tells emacs to only highlight the first subexpression (marked by \\(...\\)) using the face font-lock-type-face; t means overriding any previous highlighting.

    You can see and change the available faces with M-x customize group [RET] font-lock-faces [RET].

    C++ style casts

    I’m not sure which typename you want to be highlighted – the “original” or the one to cast into. This highlighter marks both:

    (font-lock-add-keywords 'c++-mode
      '(("\\<[[:alnum:]]+_cast[[:space:]]*<\\([[:alnum:][:space:]*]+\\)>[[:space:]]*(\\([[:alnum:][:space:]*]+\\))"
      (1 font-lock-type-face t)
      (2 font-lock-type-face t))))
    

    Again, ‘1and2` select the corresponding subexpressions.

    Please note: The regexes for the typenames do not perfectly conform to C++ syntax. For example, emacs will happily highlight nonsense like sizeof(int * 32).

    Also, my solution doesn’t take into account the problem Pavel mentioned in the comment on your question; that you could also use sizeof on variables, which would need different highlighting. I don’t think this possible, short of implementing a complete C parser in the font lock code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following config for roles using standard SqlRoleProvider, but I get an
Im using the standard java ws implementation shipped with e.g. java6 (javax.jws.*). I have
I have an application running under WebLogic that is using standard forms authentication. The
I have an app that is using standard out of the box form validation
I try to get size of GWT element with CssClass using standard methods, but
I'm fairly new to C++ standard library and have been using standard library lists
Using standard php5-pgsql driver it results to me that, at least with the default
I'm using the standard onCreateOptionsMenu, but on my Nexus w/ICS, when I tap the
I have this piece of code using standard sockets: void set_fds(int sock1, int sock2,
I have a site running MVC and Nhibernate (not fluent) using standard session per

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.