By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,369 Members | 966 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,369 IT Pros & Developers. It's quick & easy.

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

P: n/a
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();
}

Sep 15 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a
Pe*******@gmail.com 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

Sep 15 '06 #2

P: n/a
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

Sep 15 '06 #3

P: n/a
Pe*******@gmail.com 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
Sep 16 '06 #4

P: n/a
Pe*******@gmail.com 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

Sep 16 '06 #5

P: n/a
Frederick Gotham wrote:
Pe*******@gmail.com 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

Sep 16 '06 #6

P: n/a

Frederick Gotham wrote:
Pe*******@gmail.com 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

Sep 16 '06 #7

P: n/a
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
Sep 16 '06 #8

P: n/a
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
Sep 16 '06 #9

P: n/a
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

Sep 16 '06 #10

P: n/a
Frederick Gotham <fg*******@SPAM.comwrites:
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
Sep 16 '06 #11

P: n/a
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

Sep 16 '06 #12

P: n/a
Pe*******@gmail.com 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

--

Frederick Gotham
Sep 16 '06 #13

P: n/a
Frederick Gotham wrote:
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?

Sep 17 '06 #14

P: n/a
Old Wolf wrote:
Frederick Gotham wrote:
>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?
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
Sep 17 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.