Opened 13 years ago

Closed 12 years ago

#3563 closed Patches (fixed)

Warnings using g++ 4.4.0 on date_time/posix_time/conversion.hpp

Reported by: anonymous Owned by: az_sw_dude
Milestone: Boost 1.42.0 Component: date_time
Version: Boost 1.43.0 Severity: Cosmetic
Keywords: Cc:

Description

On MinGW with version 4.4.0 of "g++ -Wall -Wextra -O2":

C:/Boost/include/boost-1_40/boost/date_time/posix_time/conversion.hpp: In function 'tm boost::posix_time::to_tm(const boost::posix_time::time_duration&)': C:/Boost/include/boost-1_40/boost/date_time/posix_time/conversion.hpp:45: warning: missing initializer for member 'tm::tm_sec' C:/Boost/include/boost-1_40/boost/date_time/posix_time/conversion.hpp:45: warning: missing initializer for member 'tm::tm_min' C:/Boost/include/boost-1_40/boost/date_time/posix_time/conversion.hpp:45: warning: missing initializer for member 'tm::tm_hour' C:/Boost/include/boost-1_40/boost/date_time/posix_time/conversion.hpp:45: warning: missing initializer for member 'tm::tm_mday' C:/Boost/include/boost-1_40/boost/date_time/posix_time/conversion.hpp:45: warning: missing initializer for member 'tm::tm_mon' C:/Boost/include/boost-1_40/boost/date_time/posix_time/conversion.hpp:45: warning: missing initializer for member 'tm::tm_year' C:/Boost/include/boost-1_40/boost/date_time/posix_time/conversion.hpp:45: warning: missing initializer for member 'tm::tm_wday' C:/Boost/include/boost-1_40/boost/date_time/posix_time/conversion.hpp:45: warning: missing initializer for member 'tm::tm_yday' C:/Boost/include/boost-1_40/boost/date_time/posix_time/conversion.hpp:45: warning: missing initializer for member 'tm::tm_isdst'

Can't we explicitly initialize the tm members that should be zero'ed instead of using "timetm = {}" to get rid of this annoying warning?

Change History (6)

comment:1 by anonymous, 13 years ago

Version: Boost 1.40.0Boost 1.42.0

This is also happening on my Ubuntu-9.10 distribution with g++-4.3.2. I have boost-1.42 installed. I cannot build any code using this header (even indirectly) with -W and -Werror enabled.

This bug reproduces with the minimal source included below. Compile it with -Werror and -W:

// threadtest.cpp

#include <boost/thread.hpp>

int main(int, char **)
{
    return -1;
}

Build with

g++ -c threadtest.cpp -Werror -W -I$HOME/programs/boost-1.42/include

Compilation started at Sat Feb  6 17:29:17

g++ -c threadtest.cpp -Werror -W -I$HOME/programs/boost-1.42/include

cc1plus: warnings being treated as errors
In file included from /home/logic/programs/boost-1.42/include/boost/date_time/posix_time/conversion.hpp:17\
,
                 from /home/logic/programs/boost-1.42/include/boost/thread/xtime.hpp:15,
                 from /home/logic/programs/boost-1.42/include/boost/thread/pthread/mutex.hpp:13,
                 from /home/logic/programs/boost-1.42/include/boost/thread/mutex.hpp:16,
                 from /home/logic/programs/boost-1.42/include/boost/thread/pthread/thread_data.hpp:12,
                 from /home/logic/programs/boost-1.42/include/boost/thread/thread.hpp:17,
                 from /home/logic/programs/boost-1.42/include/boost/thread.hpp:13,
                 from threadtest.cpp:1:
/home/logic/programs/boost-1.42/include/boost/date_time/gregorian/conversion.hpp: In function 'tm boost::g\
regorian::to_tm(const boost::gregorian::date&)':
/home/logic/programs/boost-1.42/include/boost/date_time/gregorian/conversion.hpp:44: error: missing initia\
lizer for member 'tm::tm_sec'
/home/logic/programs/boost-1.42/include/boost/date_time/gregorian/conversion.hpp:44: error: missing initia\
lizer for member 'tm::tm_min'
/home/logic/programs/boost-1.42/include/boost/date_time/gregorian/conversion.hpp:44: error: missing initia\
lizer for member 'tm::tm_hour'
/home/logic/programs/boost-1.42/include/boost/date_time/gregorian/conversion.hpp:44: error: missing initia\
lizer for member 'tm::tm_mday'
/home/logic/programs/boost-1.42/include/boost/date_time/gregorian/conversion.hpp:44: error: missing initia\
lizer for member 'tm::tm_mon'
/home/logic/programs/boost-1.42/include/boost/date_time/gregorian/conversion.hpp:44: error: missing initia\
lizer for member 'tm::tm_year'
/home/logic/programs/boost-1.42/include/boost/date_time/gregorian/conversion.hpp:44: error: missing initia\
lizer for member 'tm::tm_wday'
/home/logic/programs/boost-1.42/include/boost/date_time/gregorian/conversion.hpp:44: error: missing initia\
lizer for member 'tm::tm_yday'
/home/logic/programs/boost-1.42/include/boost/date_time/gregorian/conversion.hpp:44: error: missing initia\
lizer for member 'tm::tm_isdst'
/home/logic/programs/boost-1.42/include/boost/date_time/gregorian/conversion.hpp:44: error: missing initia\
lizer for member 'tm::tm_gmtoff'
/home/logic/programs/boost-1.42/include/boost/date_time/gregorian/conversion.hpp:44: error: missing initia\
lizer for member 'tm::tm_zone'
In file included from /home/logic/programs/boost-1.42/include/boost/thread/xtime.hpp:15,
                 from /home/logic/programs/boost-1.42/include/boost/thread/pthread/mutex.hpp:13,
                 from /home/logic/programs/boost-1.42/include/boost/thread/mutex.hpp:16,
                 from /home/logic/programs/boost-1.42/include/boost/thread/pthread/thread_data.hpp:12,
                 from /home/logic/programs/boost-1.42/include/boost/thread/thread.hpp:17,
                 from /home/logic/programs/boost-1.42/include/boost/thread.hpp:13,
                 from threadtest.cpp:1:
/home/logic/programs/boost-1.42/include/boost/date_time/posix_time/conversion.hpp: In function 'tm boost::\
posix_time::to_tm(const boost::posix_time::time_duration&)':
/home/logic/programs/boost-1.42/include/boost/date_time/posix_time/conversion.hpp:46: error: missing initi\
alizer for member 'tm::tm_sec'
/home/logic/programs/boost-1.42/include/boost/date_time/posix_time/conversion.hpp:46: error: missing initi\
alizer for member 'tm::tm_min'
/home/logic/programs/boost-1.42/include/boost/date_time/posix_time/conversion.hpp:46: error: missing initi\
alizer for member 'tm::tm_hour'
/home/logic/programs/boost-1.42/include/boost/date_time/posix_time/conversion.hpp:46: error: missing initi\
alizer for member 'tm::tm_mday'
/home/logic/programs/boost-1.42/include/boost/date_time/posix_time/conversion.hpp:46: error: missing initi\
alizer for member 'tm::tm_mon'
/home/logic/programs/boost-1.42/include/boost/date_time/posix_time/conversion.hpp:46: error: missing initi\
alizer for member 'tm::tm_year'
/home/logic/programs/boost-1.42/include/boost/date_time/posix_time/conversion.hpp:46: error: missing initi\
alizer for member 'tm::tm_wday'
/home/logic/programs/boost-1.42/include/boost/date_time/posix_time/conversion.hpp:46: error: missing initi\
alizer for member 'tm::tm_yday'
/home/logic/programs/boost-1.42/include/boost/date_time/posix_time/conversion.hpp:46: error: missing initi\
alizer for member 'tm::tm_isdst'
/home/logic/programs/boost-1.42/include/boost/date_time/posix_time/conversion.hpp:46: error: missing initi\
alizer for member 'tm::tm_gmtoff'
/home/logic/programs/boost-1.42/include/boost/date_time/posix_time/conversion.hpp:46: error: missing initi\
alizer for member 'tm::tm_zone'

Compilation exited abnormally with code 1 at Sat Feb  6 17:29:20

comment:2 by Justin Randall <logicle@…>, 13 years ago

Explicit initialization isn't really a portable option, since some platforms don't define tm::tm_gmtoff or tm::tm_zone. std::memset from cstring would do the trick, and it's something used elsewhere in the boost libraries.

I have a local change from boost-trunk that fixes g++ warnings. This might not be the best fix, but it has me up and running locally without suppressing g++ warnings for my own code.

Index: posix_time/conversion.hpp
===================================================================
--- posix_time/conversion.hpp   (revision 59528)
+++ posix_time/conversion.hpp   (working copy)
@@ -8,7 +8,7 @@
  * Author: Jeff Garland, Bart Garst
  * $Date$
  */
-
+#include <cstring>
 #include <boost/date_time/posix_time/ptime.hpp>
 #include <boost/date_time/posix_time/posix_time_duration.hpp>
 #include <boost/date_time/filetime_functions.hpp>
@@ -43,7 +43,8 @@
   //! Convert a time_duration to a tm structure truncating any fractional seconds and zeroing fields for date components
   inline
   std::tm to_tm(const boost::posix_time::time_duration& td) {
-    std::tm timetm = {};
+    std::tm timetm;
+    std::memset(&timetm, 0, sizeof(timetm));
     timetm.tm_hour = date_time::absolute_value(td.hours());
     timetm.tm_min = date_time::absolute_value(td.minutes());
     timetm.tm_sec = date_time::absolute_value(td.seconds());
Index: gregorian/conversion.hpp
===================================================================
--- gregorian/conversion.hpp    (revision 59528)
+++ gregorian/conversion.hpp    (working copy)
@@ -9,6 +9,7 @@
  * $Date$
  */

+#include <cstring>
 #include <string>
 #include <stdexcept>
 #include <boost/throw_exception.hpp>
@@ -41,7 +42,8 @@
         boost::throw_exception(std::out_of_range(s));
     }

-    std::tm datetm = {}; // zero initialization is needed for extension members, like tm_zone
+    std::tm datetm;
+    std::memset(&datetm, 0, sizeof(datetm));
     boost::gregorian::date::ymd_type ymd = d.year_month_day();
     datetm.tm_year = ymd.year - 1900;
     datetm.tm_mon = ymd.month - 1;

comment:3 by Andrey Semashev, 12 years ago

(In [62681]) Silence silly GCC warnings. Refs #3563.

comment:4 by Andrey Semashev, 12 years ago

Resolution: fixed
Status: newclosed

(In [62682]) Silence silly GCC warnings. Fixes #3563.

comment:5 by anonymous, 12 years ago

Resolution: fixed
Status: closedreopened
Version: Boost 1.42.0Boost 1.43.0

Better to zero-initialize with: std::tm timetm = std::tm();

comment:6 by Andrey Semashev, 12 years ago

Resolution: fixed
Status: reopenedclosed

There is no conceptual difference. And the problem is fixed.

Note: See TracTickets for help on using tickets.