Re: SourceComment: Declare variable nearest to its first use

Yes Atul, I totally agree with you! You have a patch for that already?

Regards,
Peter

atul wrote:Dear all

I saw this style in Lippman’s C++ book
const PackedHostCaches * PackedHostCaches::findIn (const GgepBlock &ggepBlock) {
const PackedHostCaches *res = 0;
foreach (const Ggep *extension, ggepBlock.extensions()) {
res = dynamic_cast (extension);
if (res != 0)
return res;

}
return 0;
}

With

const PackedHostCaches * PackedHostCaches::findIn (const GgepBlock &ggepBlock) {

foreach (const Ggep *extension, ggepBlock.extensions()) {

if( const PackedHostCaches* res = dynamic_cast (extension) )

return res;

}

return 0;
– cheers atul

Would you like to post a relpy?


This post is a reply to:
SourceComment: Declare variable nearest to its first use
Dear all I saw this style in Lippman's C++ book const PackedHostCaches * PackedHostCaches::findIn (const GgepBlock &ggepBlock) {     const PackedHostCaches *res = 0; foreach (const Ggep *extension, ggepBlock.extensions()) {         res = dynamic_cast (more...)

Follow-ups:
Re: Re: SourceComment: Declare variable nearest to its first use
Dear Peter, Good mate, I used to use this style a lot in traversing some complex linked list code and it never failed me ;-) Will send a patch sometime today... --- cheerio (more...)