Overview Description: More detailed expansion of summary. When compiling template functions in g++ 4.0.2 (Debian 4.0.2-2) (Debian testing/unstable) with -O2 and -Wall, the compiler generates a warning about a statement with no effect. This seems to be due to the inlining with __v; as a statement, but the warning does not occur in non-template functions or with other compiler versions tried (3.4). Steps to Reproduce: Minimized, easy-to-follow steps that will trigger the bug. Include any special setup steps. 1) Create the following .cc file (as test.cc): #include <netinet/in.h> template<typename S> void serialize(const uint32_t* pitem, const S& item) { uint32_t t2 = htonl(item); } 2) Compile "test.cc" $ g++ -O2 -Wall -c -o test.o test.cc Actual Results: What the application did after performing the above steps. g++ generated a warning that the htonl statement has no effect. $ g++ -O2 -Wall -c -o test.o test.cc test.cc: In function 'void serialize(const uint32_t*, const S&)': test.cc:5: warning: statement has no effect Expected Results: What the application should have done, were the bug not present. test.o should have been created without warning Build Date & Platform: Date and platform of the build that you first encountered the bug in. >Release: 4.0.2 (Debian 4.0.2-2) (Debian testing/unstable) configured with: ../src/configure -v --enable-languages=c,c++,java,f95,objc,ada,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib--without-included-gettext --enable-threads=posix --enable-nls --program-suffix=-4.0 --enable-__cxa_atexit --enable-libstdcxx-allocator=mt --enable-clocale=gnu--enable-libstdcxx-debug --enable-java-gc=boehm --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-4.0-1.4.2.0/jre --enable-mpfr --disable-werror --enable-checking=release i486-linux-gnu >Environment: System: Linux killian.ucsd.edu 2.6.12.20051019-ck1 #1 SMP Wed Oct 19 09:24:47 PDT 2005 i686 GNU/Linux Architecture: i686 Additional Builds and Platforms: Whether or not the bug takes place on other platforms (or browsers, if applicable). - Also Occurs On Unknown - Doesn't Occur On g++ version 3.4.4 20050721 (Red Hat 3.4.4-2) Or without -O2, -Wall, or outside of a template function.
Created attachment 10055 [details] The test.cc file
Created attachment 10056 [details] preprocessor output
I believe this winds up being a duplicate of PR c++/8057, which is fixed on mainline. With the test case in the PR, I see the warning with 4.0, but I do not see the warning on mainline. Note that this slightly modified test case: #include <netinet/in.h> template<typename S> void serialize(const uint32_t* pitem, const S& item) { uint32_t t2 = htonl(item); } template void serialize<int> (const uint32_t*, const int&); issues a warning on both 4.0 and mainline: foo.cc: In function ‘void serialize(const uint32_t*, const S&) [with S = int]’: foo.cc:8: instantiated from here foo.cc:5: warning: unused variable ‘t2’
(In reply to comment #3) > I believe this winds up being a duplicate of PR c++/8057, which is fixed on > mainline. With the test case in the PR, I see the warning with 4.0, but I do > not see the warning on mainline. > > Note that this slightly modified test case: > > #include <netinet/in.h> > > template<typename S> > void serialize(const uint32_t* pitem, const S& item) { > uint32_t t2 = htonl(item); > } > > template void serialize<int> (const uint32_t*, const int&); > > issues a warning on both 4.0 and mainline: > > foo.cc: In function ‘void serialize(const uint32_t*, const S&) [with S = int]’: > foo.cc:8: instantiated from here > foo.cc:5: warning: unused variable ‘t2’ > The unused variable warning of course is behavior as expected for this small sample. My original statement that the sample should compile without warning should seems not to be correct. That could be corrected by using the variable, for example by printing it. The motivating example appended the bytes of t2 to a std::string which was passed in by reference.
Here is a reduced testcase: template <int > void f(int i) { int i1 = (__extension__ ({int i2 = i; i2;})); }
(In reply to comment #3) > I believe this winds up being a duplicate of PR c++/8057, which is fixed on > mainline. With the test case in the PR, I see the warning with 4.0, but I do > not see the warning on mainline. I don't think this is a duplicate of PR 8057 because my reduced testcase worked in 3.4.5. I think this was introduced by one of the C++ patches between 4.0.1 and 4.0.2 > Or without -O2, -Wall, or outside of a template function. the without -O2, is because the headers change, which is why my simplified example shows the issue at every optimization level.
Fixed in GCC-4.1.0 and higher