Opened 6 years ago

Closed 5 years ago

#12940 closed Bugs (fixed)

Missing initializer for member

Reported by: mattyclarkson@… Owned by: timblechmann
Milestone: To Be Determined Component: atomic
Version: Boost 1.63.0 Severity: Problem
Keywords: Cc: Andrey.Semashev@…

Description

I have the following error when compiling with my built boost 1.63:

/usr/include/boost/atomic/detail/bitwise_cast.hpp: In instantiation of 'To boost::atomics::detail::bitwise_cast(const From&) [with To = long unsigned int; From = void*]':
/usr/include/boost/atomic/detail/atomic_template.hpp:556:139:   required from here
/usr/include/boost/atomic/detail/bitwise_cast.hpp:39:14: error: missing initializer for member 'boost::atomics::detail::bitwise_cast(const From&) [with To = long unsigned int; From = void*]::<anonymous struct>::to' [-Werror=missing-field-initializers]
     value = {};

The code looks like:

template< typename To, typename From >
BOOST_FORCEINLINE To bitwise_cast(From const& from) BOOST_NOEXCEPT
{
    struct
    {
        To to;
    }
    value = {};
    BOOST_ATOMIC_DETAIL_MEMCPY
    (
        &reinterpret_cast< char& >(value.to),
        &reinterpret_cast< const char& >(from),
        (sizeof(From) < sizeof(To) ? sizeof(From) : sizeof(To))
    );
    return value.to;
}

My proposed fix is:

template< typename To, typename From >
BOOST_FORCEINLINE To bitwise_cast(From const& from) BOOST_NOEXCEPT
{
    struct Value
    {
        To to;
        Value() : to() {}
    }
    struct Value value;
    BOOST_ATOMIC_DETAIL_MEMCPY
    (
        &reinterpret_cast< char& >(value.to),
        &reinterpret_cast< const char& >(from),
        (sizeof(From) < sizeof(To) ? sizeof(From) : sizeof(To))
    );
    return value.to;
}

Attachments (1)

0002-Avoid-missing-field-initializzer-warning.patch (1007 bytes ) - added by mattyclarkson@… 6 years ago.
Proposed git format-patch for downloaded source

Download all attachments as: .zip

Change History (2)

by mattyclarkson@…, 6 years ago

Proposed git format-patch for downloaded source

comment:1 by Andrey Semashev, 5 years ago

Cc: Andrey.Semashev@… added
Resolution: fixed
Status: newclosed

Not all compilers support value initialization properly, so the proposed solution is not quite portable.

The warning should be fixed in https://github.com/boostorg/atomic/commit/078639812015b96f64eb88a67a857d12201aab17.

Note: See TracTickets for help on using tickets.