public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/15083] New: spurious "statement has no effect" warning
@ 2004-04-22 23:23 carlton at bactrian dot org
  2004-04-22 23:38 ` [Bug c++/15083] " carlton at bactrian dot org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: carlton at bactrian dot org @ 2004-04-22 23:23 UTC (permalink / raw)
  To: gcc-bugs

When I compile the following program with g++ -Wall (where g++ is the
released 3.4.0), I get the following warning:

quartette$ /net/quartette/carlton/gcc/gcc-3.4.0/install/bin/g++ -Wall test.cpp
test.cpp: In member function `void Foo<T>::resetData()':
test.cpp:31: warning: statement has no effect

The line in question is this one:

        new ( refLoc ) Counter();

but Counter's contructor does have a side effect.  (Indeed, the output
of the program changes if you compile out that line.)

Here's the source code; I'll attach a .ii file as well.  (The .ii
file is kind of large, because I include <iostream> to show that
the line in question does indeed change the behavior of the program.)

#include <new>
#include <iostream>

class Counter {
public:
    void decrement() { --ref_; }

    Counter() : ref_(1) {}

    int ref() const {
        return ref_;
    }

private:
    int ref_;
};

template< typename T >
class Foo {
  public:
    void resetData() {
        ref_.decrement();
        std::cout << "before: " << ref_.ref() << std::endl;
        ref_.~Counter();
        void *refLoc = &ref_;

        // GCC erroneously claims that the following line has no
        // effect.  But it does have an effect: the value of the
        // "after" message will be 0 instead of 1 if you comment it
        // out.
        new ( refLoc ) Counter();

        std::cout << "after: " << ref_.ref() << std::endl;
    }

  private:
    Counter ref_;
};

int main() {
    Foo< int > node;

    node.resetData();
}

-- 
           Summary: spurious "statement has no effect" warning
           Product: gcc
           Version: 3.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: carlton at bactrian dot org
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15083


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2004-05-28 23:35 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-04-22 23:23 [Bug c++/15083] New: spurious "statement has no effect" warning carlton at bactrian dot org
2004-04-22 23:38 ` [Bug c++/15083] " carlton at bactrian dot org
2004-04-22 23:43 ` carlton at bactrian dot org
2004-04-22 23:46 ` carlton at bactrian dot org
2004-04-22 23:49 ` carlton at bactrian dot org
2004-04-22 23:51 ` [Bug c++/15083] [3.4/3.5 Regression]spurious " pinskia at gcc dot gnu dot org
2004-05-06 23:23 ` pinskia at gcc dot gnu dot org
2004-05-06 23:26 ` pinskia at gcc dot gnu dot org
2004-05-29 16:19 ` mmitchel at gcc dot gnu dot org
2004-05-30  1:58 ` cvs-commit at gcc dot gnu dot org
2004-05-30  3:58 ` cvs-commit at gcc dot gnu dot org
2004-05-30  7:49 ` mmitchel at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).