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

Discussion in 'C++' started by PengYu.UT@gmail.com, Sep 15, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. 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.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
     
    Thomas Tutone, Sep 15, 2006
    #2
    1. Advertising

  3. Guest

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


    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 16, 2006
    #3
  4. 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
     
    Frederick Gotham, Sep 16, 2006
    #4
  5. 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.1.1/gcc/Invoking-GCC.html#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.4.6/gcc/index.html#toc_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
     
    Thomas Tutone, Sep 16, 2006
    #5
  6. Kai-Uwe Bux Guest

    Frederick Gotham wrote:

    > 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
     
    Kai-Uwe Bux, Sep 16, 2006
    #6
  7. Frederick Gotham wrote:
    > 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
     
    Thomas Tutone, Sep 16, 2006
    #7
  8. 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
     
    Frederick Gotham, Sep 16, 2006
    #8
  9. 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
     
    Frederick Gotham, Sep 16, 2006
    #9
  10. 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
     
    Thomas Tutone, Sep 16, 2006
    #10
  11. Jens Theisen Guest

    Frederick Gotham <> writes:

    > 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
     
    Jens Theisen, Sep 16, 2006
    #11
  12. Guest

    > 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, 2006
    #12
  13. 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
     
    Frederick Gotham, Sep 16, 2006
    #13
  14. Old Wolf Guest

    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?
     
    Old Wolf, Sep 17, 2006
    #14
  15. Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, Sep 18, 2006
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jacol

    raise or not to raise [Newbie]

    Jacol, Feb 3, 2007, in forum: Python
    Replies:
    5
    Views:
    416
    Gabriel Genellina
    Feb 5, 2007
  2. ww
    Replies:
    4
    Views:
    403
    James Kanze
    Oct 26, 2007
  3. ernest
    Replies:
    2
    Views:
    297
    Roy Smith
    Nov 14, 2010
  4. Jack Bates
    Replies:
    0
    Views:
    284
    Jack Bates
    May 2, 2011
  5. bvdp

    Raise X or Raise X()?

    bvdp, Mar 11, 2012, in forum: Python
    Replies:
    10
    Views:
    383
    Stefan Behnel
    Mar 12, 2012
Loading...

Share This Page