[Boost-bugs] [Boost C++ Libraries] #11514: Thread-unsafe (or non-optimal in C++11) static initialization in boost/spirit/home/qi/numeric/detail/numeric_utils.hpp

Subject: [Boost-bugs] [Boost C++ Libraries] #11514: Thread-unsafe (or non-optimal in C++11) static initialization in boost/spirit/home/qi/numeric/detail/numeric_utils.hpp
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2015-07-27 16:56:12


#11514: Thread-unsafe (or non-optimal in C++11) static initialization in
boost/spirit/home/qi/numeric/detail/numeric_utils.hpp
------------------------------------------------+---------------------
 Reporter: Artem Tokmakov <artyom.tokmakov@…> | Owner: djowel
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: spirit
  Version: Boost 1.58.0 | Severity: Problem
 Keywords: unsafe initialization |
------------------------------------------------+---------------------
 This file (it's the same in boost 1.58 which I have too):
 boost/spirit/home/qi/numeric/detail/numeric_utils.hpp has following code
 (similar in a couple of places):


 {{{
 // Ensure n *= Radix will not overflow
 static T const max = (std::numeric_limits<T>::max)();
 static T const val = max / Radix;
 }}}

 Since static initialization is not thread-safe in Visual Studio (the one
 I'm using now is 2013 update 4), and because these variables are dependent
 on each other, this causes problems. Indeed, sometimes I get '''val'''
 initialized to 0, and other times to correct value.

 But even when "magic statics" are implemented, this will probably be not
 the most efficient implementation since a better one would be using, say,
 constexpr.

 There are couple of ways to fix this, and one with minimal changes
 (considering my minimal knowledge of boost constexpr machinery) would be:

 {{{
 // ## Note how two variables are initialized independently.
 static T const max = (std::numeric_limits<T>::max)();
 static T const val = (std::numeric_limits<T>::max)() / Radix;
 }}}

 Thanks!

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/11514>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:18 UTC