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

P

PengYu.UT

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();
}
 
T

Thomas Tutone

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.1.1/gcc/Invoking-GCC.html#Invoking-GCC
for gcc 4.1.1) or a mailing list or forum for that compiler.

Best regards,

Tom
 
F

Frederick Gotham

(e-mail address 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.
 
K

Kai-Uwe Bux

Frederick said:
(e-mail address removed) 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.

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
 
T

Thomas Tutone

Frederick said:
(e-mail address removed) posted:



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
 
F

Frederick Gotham

Kai-Uwe Bux posted:
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.

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.
 
F

Frederick Gotham

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.
 
T

Thomas Tutone

Frederick said:
Thomas Tutone posted:



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.

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
 
J

Jens Theisen

Frederick Gotham said:
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.

One of the points was presumably that your own style of answering
wasn't friendly at all - in addition to that it was not helpful at
all.

His idea of compiler warnings in such cases is - if possible - just
sensible. After all, one of the primary strenghs of C++ over where it
came from is type safety - something which points in the same
direction.

Jens
 
P

PengYu.UT

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

I agree programmers should reduce bugs if they can. However,
programmers are humans. Humans can make mistakes, which they are not
even aware of. The compiler should help programmer as much as possible.
It should help programmer reduce their labors. I think, for the
particular example that I gave, it should be not difficult that the
compiler generate some warning messages.

Thanks,
Peng
 
F

Frederick Gotham

(e-mail address removed) posted:
I agree programmers should reduce bugs if they can. However,
programmers are humans. Humans can make mistakes, which they are not
even aware of. The compiler should help programmer as much as possible.
It should help programmer reduce their labors. I think, for the
particular example that I gave, it should be not difficult that the
compiler generate some warning messages.


Given the following code:

int &Func()
{
int i = 5;
return i;
}

int main()
{
Func() = 7;
}

, my own compiler says:

3 K:\main.cpp [Warning] reference to local variable `i' returned
 
O

Old Wolf

Frederick said:
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.

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

I agree entirely! I propose that in order to lighten the burden on the
C++ standards committee, that the Standard be amended to never
require a diagnostic. After all, if a programmer makes a mistake,
it's hardly C++'s fault, is it?
 
K

Kai-Uwe Bux

Old said:
I agree entirely! I propose that in order to lighten the burden on the
C++ standards committee, that the Standard be amended to never
require a diagnostic. After all, if a programmer makes a mistake,
it's hardly C++'s fault, is it?

Ahem, no need for change. The following is compiler behavior is standard
compliant: upon completion, the compiler generates just one diagnostic
message telling you: "there may or may not be an error in your code". It
will always produce an executable. In the case of an ill-formed program the
behavior is undefined.


Best

Kai-Uwe Bux
 

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
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top