Programming style section from Josuttis' C++ template book

K

KPB

I have an O'Reilly Safari Online subscription. I decided to check out
this book(C++ Templates) and am half way through it.

For those of you who read this one, I don't know what you thought about
section 1.4 (Some remarks about Programming Style) but I thought it was
weird at first.

You know? Writing an init statement like this:

int const N = 100;

instead of like this (which I've always done)

const int N = 100;


I have to say though, the style outlined in the book has really grown on
me. It really makes sense when you throw a reference token or pointer
token in there. Just read it from right to left

char const* const p = 0;

I know from reading this right to left it's a "constant pointer" to a
"constant character".

Using this way as I always have done:

const char* const p = 0;

I always had to think about it for a second.

I see the authors' points about this. Did any of you are do you still
think it's *weird*?

Just curious.

KPB
 
D

Dave O'Hearn

KPB said:
[...]
int const N = 100;

instead of like this (which I've always done)

const int N = 100;

I have to say though, the style outlined in the book has really
grown on me. It really makes sense when you throw a reference
token or pointer token in there. Just read it from right to left
[...]
I see the authors' points about this. Did any of you are do you
still think it's *weird*?

I find it makes declarations harder to read "at a glance", but easier
to untangle when I have to read them one step at a time. If English
were not my native language, I might find (char const *) more natural,
but in English, I am used to adjectives coming first.

I've found that I prefer the style from the book, and I experimented
with it in a small tool I wrote, and it's not hard to get used to. I'm
unsure I would use it in real code, though.
 
K

KPB

Dave said:
KPB wrote:
I've found that I prefer the style from the book, and I experimented
with it in a small tool I wrote, and it's not hard to get used to. I'm
unsure I would use it in real code, though.

I feel the same way. Not sure if I'll start using this at work myself.

When I first read this, I was thinking "This is bullshit details! Let's
get to the good stuff!" I hadn't realized how cool this little detail was.

Yes.. the rest of the book is great too so far. :)
 
B

Bruce Trask

KPB,

Yes, it took me a while to recalibrate to this small shift of putting the
const after the type. The section of 1.4 in that book on syntactical
substitution principle was quite interesting in that it shows a case where
putting the const *before* the type can actually cause confusion if someone
thinks you can just textually replace a typedef.

Also in favor of putting the const after the type it modifies is that is how
compilers view it so if you wrote a function void func(const char* p), and
then viewed the symbols generated by the compiler using what every binary
utils are available for your compiler, you will see that the compiler thinks
of that function as void func(char const* p). Can be usesful to think this
way to decipher compiler error messages. Matter of taste of course but
interesting to view if both ways.

Regards,
Bruce
 
K

KPB

Bruce said:
The section of 1.4 in that book on syntactical
substitution principle was quite interesting in that it shows a case where
putting the const *before* the type can actually cause confusion if someone
thinks you can just textually replace a typedef.

Actually, I think I experienced a similar typedef problem at work.
However, I was an hour away from Xmas vacation so didn't pay much
attention to it. I'll have to look at it again when I get back.

BTW, ... BAE Greenlawn?

Thanks,
KPB
 
B

Bruce Trask

KPB,
Actually, I think I experienced a similar typedef problem at work.
However, I was an hour away from Xmas vacation so didn't pay much
attention to it. I'll have to look at it again when I get back.

Right, at first I thought I groked that section fully but really had to work
some examples until I got it.
BTW, ... BAE Greenlawn?

Greenlawn and Wayne NJ. Mostly Wayne.

Regards,
Bruce
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,298
Messages
2,571,540
Members
48,275
Latest member
tetedenuit01

Latest Threads

Top