Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. "const int" or "int const" ?

"const int" or "int const" ?

Scheduled Pinned Locked Moved C / C++ / MFC
c++htmlcsscollaborationquestion
3 Posts 2 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gavin Greig
    wrote on last edited by
    #1

    Both the following are valid C++ and have the same meaning:

    const int i = 0;
    int const j = 0;

    The first style is widely used, and everyone will recognise and understand it. The second style is not so immediately understandable to most developers. However, always putting the const qualifier immediately to the right of what it qualifies can prevent some esoteric bugs. There's a brief discussion of why, with examples, here[^], in the section titled Avoiding confusion by using a good programming style. I only found a few other examples when I searched a few weeks ago, so it's safe to assume the second method is not widely known. In your opinion, is the safer, but less well-known and understood style "better" than the widely-used one, or not? For what it's worth, I think the safer style is "better", but obviously changing such a deeply engrained style across a development team or organisation is likely to prove difficult. Gavin Greig "Haw, you're no deid," girned Charon. "Get aff ma boat or ah'll report ye." Matthew Fitt - The Hoose O Haivers: The Twelve Trauchles O Heracles.

    M 1 Reply Last reply
    0
    • G Gavin Greig

      Both the following are valid C++ and have the same meaning:

      const int i = 0;
      int const j = 0;

      The first style is widely used, and everyone will recognise and understand it. The second style is not so immediately understandable to most developers. However, always putting the const qualifier immediately to the right of what it qualifies can prevent some esoteric bugs. There's a brief discussion of why, with examples, here[^], in the section titled Avoiding confusion by using a good programming style. I only found a few other examples when I searched a few weeks ago, so it's safe to assume the second method is not widely known. In your opinion, is the safer, but less well-known and understood style "better" than the widely-used one, or not? For what it's worth, I think the safer style is "better", but obviously changing such a deeply engrained style across a development team or organisation is likely to prove difficult. Gavin Greig "Haw, you're no deid," girned Charon. "Get aff ma boat or ah'll report ye." Matthew Fitt - The Hoose O Haivers: The Twelve Trauchles O Heracles.

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      "const int" and "int const" are absolutely identical in meaning. I've literally never seen "int const" outside of reference books and web pages. The "avoiding confusion" section in the quoted page is actually about programming style, which is a highly personal issue. --Mike-- Latest blog entry: *drool* (Alyson) [May 10] Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber "You have Erica on the brain" - Jon Sagara to me

      G 1 Reply Last reply
      0
      • M Michael Dunn

        "const int" and "int const" are absolutely identical in meaning. I've literally never seen "int const" outside of reference books and web pages. The "avoiding confusion" section in the quoted page is actually about programming style, which is a highly personal issue. --Mike-- Latest blog entry: *drool* (Alyson) [May 10] Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber "You have Erica on the brain" - Jon Sagara to me

        G Offline
        G Offline
        Gavin Greig
        wrote on last edited by
        #3

        Maybe my examples weren't best chosen. As the article I referred to points out, the position of the const qualifier can become significant when you introduce a pointer or reference. So although "const int" and "int const" are identical, "const int*" and "int* const" are not. The first is a pointer to a const int, while the second is a const pointer to a non-const int. Of course, you might not make this error if you separate the pointer from the type, because "const int *" and "int const *" are also identical in meaning. Thinking in terms of always putting const to the right of what you are making const would help to avoid this sort of confusion. Now, if you are using typedefs, it can be more confusing still:

        typedef int * PINT;
        typedef const PINT CPINT; // const pointer to int
        typedef const int * PCINT; // pointer to const int

        It might be "reasonable" to expect, when attempting to understand line 2 that the type defined in line 3 would be identical, because all we've done is substitute "int *" for the typedef PINT - but the result is not the same. However, if you adopt the "const to the right" convention, then making that direct substitution does work:

        typedef int * PINT;
        typedef PINT const CPINT; // const pointer to int
        typedef int * const CPINT; // const pointer to int

        Of course, you could say that you'll never write such a typedef, but the typedef could be created by someone else within a template - Loki makes use of typedefs in its Typelists, for example - and if you're not using the "const to the right" convention then it will be harder to understand and debug any resulting issues. So yes, it is a style issue, but there is at least one specific circumstance in which it will commonly lead to an incorrect understanding of the way C++ works, and poor understanding leads directly to bugs. What I was really hoping for were some opinions as to whether an improvement in long term understandability of code justified the short term confusion that would be inevitable by overturning a long-standing convention? Reference: "C++ Templates - The Complete Guide", David Vandevoorde and Niccolai Josuttis. There is also a short article (c.1998), available somewhere online, which seems to have been the first to raise this issue, but unfortunately I haven't managed to find it again to provide a link. Gavin Greig "Haw, you're no deid," girned Charon. "Get aff ma boat or ah'll report ye." M

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups