Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to let C++ compiler raise a warning or error when a const is assigned to a const reference.

Reply
Thread Tools

How to let C++ compiler raise a warning or error when a const is assigned to a const reference.

 
 
PengYu.UT@gmail.com
Guest
Posts: n/a
 
      09-15-2006
Hi,

I have the following which has a bug (see the commented line).

The one with the bug has the output of
10
-1073752704

The one without the bug has the output of
10
10

However, the compiler that I'm using can't generate any warning or
error for this bug. I'm wondering if there is a option to select such
that the compiler can tell this error?

Thanks,
Peng

#include <iostream>

class A{
public:
A(const int a) : _a(a) { //should be: A(const int &a) : _a(a) {
}
void print() const {
std::cout << _a << std::endl;
}
private:
const int &_a;
};

class B{
public:
B(const A &a) : _a(a) {
};
void print() const {
_a.print();
}
private:
const A &_a;
};

int main(){
int i = 10;
A a(i);
a.print();
B b(a);
b.print();
}

 
Reply With Quote
 
 
 
 
Thomas Tutone
Guest
Posts: n/a
 
      09-15-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:


> I have the following which has a bug (see the commented line).
>
> The one with the bug has the output of
> 10
> -1073752704
>
> The one without the bug has the output of
> 10
> 10
>
> However, the compiler that I'm using can't generate any warning or
> error for this bug. I'm wondering if there is a option to select such
> that the compiler can tell this error?


Perhaps so, perhaps not. Since you don't identify the compiler,
though, how could anyone help you? Your best bet is to consult the
documentation for the compiler (e.g.,
http://gcc.gnu.org/onlinedocs/gcc-4....l#Invoking-GCC
for gcc 4.1.1) or a mailing list or forum for that compiler.

Best regards,

Tom

 
Reply With Quote
 
 
 
 
PengYu.UT@gmail.com
Guest
Posts: n/a
 
      09-15-2006
> Perhaps so, perhaps not. Since you don't identify the compiler,
> though, how could anyone help you? Your best bet is to consult the
> documentation for the compiler (e.g.,
> http://gcc.gnu.org/onlinedocs/gcc-4....l#Invoking-GCC
> for gcc 4.1.1) or a mailing list or forum for that compiler.


I'm using gcc-3.4. It is too long to read the GCC documentation. Do you
know which section I should read?

Thanks,
Peng

 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      09-16-2006
(E-Mail Removed) posted:

> A(const int a) : _a(a) { //should be: A(const int &a) : _a(a)



The only bug you have is:

"You didn't read back over your code."

I would like to think that I produce quality code, but from time to time, I
post flawed code. The difference between such flawed code and _real_ code
that I produce is that I read back over my code.

If you can't read back over your code and pay attention to the placement (or
lack thereof) of asterisks and ampersands, then take up botany.

--

Frederick Gotham
 
Reply With Quote
 
Thomas Tutone
Guest
Posts: n/a
 
      09-16-2006
(E-Mail Removed) wrote:
> > Perhaps so, perhaps not. Since you don't identify the compiler,
> > though, how could anyone help you? Your best bet is to consult the
> > documentation for the compiler (e.g.,
> > http://gcc.gnu.org/onlinedocs/gcc-4....l#Invoking-GCC
> > for gcc 4.1.1) or a mailing list or forum for that compiler.

>
> I'm using gcc-3.4. It is too long to read the GCC documentation. Do you
> know which section I should read?


<OT/>

http://gcc.gnu.org/onlinedocs/gcc-3....c_Invoking-GCC

Given that I gave you the url for gcc 4.1.1, I think you could have
figured this one out for yourself.

</OT>

Best regards,

Tom

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      09-16-2006
Frederick Gotham wrote:

> (E-Mail Removed) posted:
>
>> A(const int a) : _a(a) { //should be: A(const int &a) : _a(a)

>
>
> The only bug you have is:
>
> "You didn't read back over your code."


The OP is not guilty of the charge: obviously, the OP did read back over his
code and spotted the bug; after all, he posted a correct version, too. It
even shows in the comment you quote.

His question concerns strictly whether the compiler, with appropriate
settings, will help detecting this kind of error (just as many compilers
warn about type mismatches in print() or assignments in if clauses).


> I would like to think that I produce quality code, but from time to time,
> I post flawed code. The difference between such flawed code and _real_
> code that I produce is that I read back over my code.
>
> If you can't read back over your code and pay attention to the placement
> (or lack thereof) of asterisks and ampersands, then take up botany.


Your attitude is misplaced here.


Best

Kai-Uwe Bux

 
Reply With Quote
 
Thomas Tutone
Guest
Posts: n/a
 
      09-16-2006

Frederick Gotham wrote:
> (E-Mail Removed) posted:
>
> > A(const int a) : _a(a) { //should be: A(const int &a) : _a(a)

>
>
> The only bug you have is:
>
> "You didn't read back over your code."
>
> I would like to think that I produce quality code, but from time to time, I
> post flawed code. The difference between such flawed code and _real_ code
> that I produce is that I read back over my code.
>
> If you can't read back over your code and pay attention to the placement (or
> lack thereof) of asterisks and ampersands, then take up botany.


Fred, this post speaks worlds about you.

Best regards,

Tom

 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      09-16-2006
Kai-Uwe Bux posted:

>> The only bug you have is:
>>
>> "You didn't read back over your code."

>
> The OP is not guilty of the charge: obviously, the OP did read back over
> his code and spotted the bug; after all, he posted a correct version,
> too. It even shows in the comment you quote.



Yes, yes, I realise that.

My post poked at the core reasoning for which the original post was posted
-- the original poster wished to be advised of when they made such a
mistake. My own opinion is that _they_ _themselves_ should correct such a
mistake after having read back over the code. They shouldn't rely on the
compiler to point out every bug in their code.

I myself write buggy code all the time! I'm actually quite good at it! Give
me an objective, and give me half an hour, and I'll produce wonderfully
elaborate code which probably contains a few bugs.

My "final code" is different, however, as I read back over it slowly and
intuitively, correcting any typos and/or errors.

I acknowledge my humanity, and thus acknowledge my uncanny ability to make
mistakes -- but I also assert my ability to correct any such mistakes after
having read back over my code.


> His question concerns strictly whether the compiler, with appropriate
> settings, will help detecting this kind of error (just as many compilers
> warn about type mismatches in print() or assignments in if clauses).



Compiler warnings are a good thing -- but they're no substitute for a
competant programmer.


>> I would like to think that I produce quality code, but from time to
>> time, I post flawed code. The difference between such flawed code and
>> _real_ code that I produce is that I read back over my code.
>>
>> If you can't read back over your code and pay attention to the
>> placement (or lack thereof) of asterisks and ampersands, then take up
>> botany.

>
> Your attitude is misplaced here.



Forgive me if I came across abrasive, but my intent was to influence the
original poster to take responsibility for their own mistakes.

--

Frederick Gotham
 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      09-16-2006
Thomas Tutone posted:

> Fred, this post speaks worlds about you.



Thomas, you're not the first person to make one-sentence-diatribes about me.
If you would like to elaborate on your statement, I would be glad to engange
in friendly discussion with you pertaining to the fashion in which I post.

--

Frederick Gotham
 
Reply With Quote
 
Thomas Tutone
Guest
Posts: n/a
 
      09-16-2006
Frederick Gotham wrote:

> Thomas Tutone posted:
>
> > Fred, this post speaks worlds about you.

>
>
> Thomas, you're not the first person to make one-sentence-diatribes about me.
> If you would like to elaborate on your statement, I would be glad to engange
> in friendly discussion with you pertaining to the fashion in which I post.
>
> --
>
> Frederick Gotham


Fair enough. My email address is a matter of public record. Feel free
to contact me off-usenet if you want to engage in such a discussion.

Best regards,

Tom

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Raise X or Raise X()? bvdp Python 10 03-12-2012 04:08 PM
"raise (type, value, traceback)" and "raise type, value, traceback" Jack Bates Python 0 05-02-2011 05:23 PM
raise Exception or raise Exception() ernest Python 2 11-14-2010 08:14 PM
raise or not to raise [Newbie] Jacol Python 5 02-05-2007 11:46 PM



Advertisments