Compiling the following program:

int printf(const char *fmt, ...);

class MyCompare {
 public:
  MyCompare() { }
  bool operator() (const int a, const int b) const {
    return a > b;
  }
};

template<class Comp>
class Other {
 public:
  Other (const Comp &c) : c_(c) { }
  void* doSomething();

 private:
  Comp c_;
};

template<class Comp>
void* Other<Comp>::doSomething() {
  return &c_;
}

int main(int argc, char **argv) {
  MyCompare c;
  Other<MyCompare> other(c);
  printf("%p\n", other.doSomething());
  return 0;
}

yields the following warning:

test.cc: In function 'int main(int, char**)':
test.cc:14: warning: 'c' is used uninitialized in this function
test.cc:27: note: 'c' was declared here

when compiled with the following command line:

g++ -Wall -c -O2 test.cc

However, c is initialized by the no argument constructor (further class
MyCompare does not actually have any member variables to initialize).


-- 
           Summary: Compiler warns about uninitialized variable that is an
                    object with a constructor
           Product: gcc
           Version: 4.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: nvachhar at google dot com
 GCC build triplet: i686-unknown-linux-gnu
  GCC host triplet: i686-unknown-linux-gnu
GCC target triplet: i686-unknown-linux-gnu


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

Reply via email to