Click here to Skip to main content
15,906,463 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionNTFS file tagging? Pin
rcsj122-May-03 15:38
rcsj122-May-03 15:38 
AnswerRe: NTFS file tagging? Pin
Michael Dunn22-May-03 18:52
sitebuilderMichael Dunn22-May-03 18:52 
GeneralSplit String Question.... Pin
Nitron22-May-03 13:49
Nitron22-May-03 13:49 
GeneralRe: Split String Question.... Pin
Joaquín M López Muñoz22-May-03 20:05
Joaquín M López Muñoz22-May-03 20:05 
GeneralRe: Split String Question.... Pin
Nitron23-May-03 2:16
Nitron23-May-03 2:16 
Question"const int" or "int const" ? Pin
Gavin Greig22-May-03 12:57
Gavin Greig22-May-03 12:57 
AnswerRe: "const int" or "int const" ? Pin
Michael Dunn22-May-03 18:57
sitebuilderMichael Dunn22-May-03 18:57 
GeneralRe: "const int" or "int const" ? Pin
Gavin Greig22-May-03 22:58
Gavin Greig22-May-03 22:58 
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."
Matthew Fitt - The Hoose O Haivers: The Twelve Trauchles O Heracles.

Generalvswprintf Pin
Matt Newman22-May-03 12:57
Matt Newman22-May-03 12:57 
GeneralRe: vswprintf Pin
cmk22-May-03 14:45
cmk22-May-03 14:45 
GeneralRe: vswprintf Pin
Matt Newman22-May-03 16:54
Matt Newman22-May-03 16:54 
GeneralRe: vswprintf Pin
cmk23-May-03 8:18
cmk23-May-03 8:18 
QuestionGetting WebBrowser app to use Sun's VM? Pin
gmontem22-May-03 12:20
gmontem22-May-03 12:20 
GeneralRich Edit control programming in C Pin
SAK22-May-03 11:32
SAK22-May-03 11:32 
GeneralRe: Rich Edit control programming in C Pin
jhaga22-May-03 12:04
professionaljhaga22-May-03 12:04 
GeneralRe: Rich Edit control programming in C Pin
SAK23-May-03 5:35
SAK23-May-03 5:35 
GeneralValidate a filename Pin
JC Gauthier22-May-03 10:35
JC Gauthier22-May-03 10:35 
GeneralRe: Validate a filename Pin
valikac22-May-03 11:16
valikac22-May-03 11:16 
GeneralRe: Validate a filename Pin
Anonymous23-May-03 3:32
Anonymous23-May-03 3:32 
GeneralRe: Validate a filename Pin
valikac23-May-03 4:11
valikac23-May-03 4:11 
GeneralRe: Validate a filename Pin
gmontem22-May-03 12:17
gmontem22-May-03 12:17 
GeneralEAP dll, IEAPProviderConfig and samples Pin
aguacate22-May-03 10:25
aguacate22-May-03 10:25 
GeneralImage Magick Demo project Pin
tulip_tulip6822-May-03 9:54
tulip_tulip6822-May-03 9:54 
GeneralBitmap Dlg Pin
MemLeak22-May-03 9:26
MemLeak22-May-03 9:26 
GeneralAccessing structure problems Pin
act_x22-May-03 9:11
act_x22-May-03 9:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.