electronicarts / EASTL

EASTL stands for Electronic Arts Standard Template Library. It is an extensive and robust implementation that has an emphasis on high performance.
BSD 3-Clause "New" or "Revised" License
8.09k stars 929 forks source link

eastl::optional::value() casts away qualifiers [-Werror=cast-qual] #531

Open SGTech87 opened 4 months ago

SGTech87 commented 4 months ago

In this example, if I use GCC and the "-Wcast-qual" is treated as an error:

#include <EASTL/optional.h>
#include <optional>

struct S
{
    std::optional<int>   opt_std;
    eastl::optional<int> opt_eastl;

    int get_std() const
    {
        return opt_std.has_value() ? opt_std.value() : 0;
    }

    int get_eastl() const
    {
        return opt_eastl.has_value() ? opt_eastl.value() : 0;
    }
};

int main()
{
    S opt_wrap;
    const int std_val = opt_wrap.get_std();
    const int eastl_val = opt_wrap.get_eastl();

    return 0;
}

I get a compilation error from EASTL optional.h:

include/EASTL/optional.h: In instantiation of 'eastl::optional<T>::value_type& eastl::optional<T>::get_value_ref() const [with T = int; eastl::optional<T>::value_type = int]':
include/EASTL/optional.h:351:64:   required from 'const T& eastl::optional<T>::value() const & [with T = int]'
main.cpp:16:56:   required from here
include/EASTL/optional.h:468:15: error: cast from type 'const eastl::aligned_storage<4, 4>::type*' to type 'eastl::optional<int>::value_type*' {aka 'int*'} casts away qualifiers [-Werror=cast-qual]
       return *(value_type*)eastl::addressof(val);

It seems like there is an implementation issue in eastl::optional, whereas std::optional works fine. Maybe a const is missing from the cast:

inline const value_type& get_value_ref() const EASTL_OPTIONAL_NOEXCEPT
{
    #if EASTL_EXCEPTIONS_ENABLED
        if(!engaged)
            throw bad_optional_access();
    #elif EASTL_ASSERT_ENABLED
        EASTL_ASSERT_MSG(engaged, "no value to retrieve");
    #endif
    return *(value_type*)eastl::addressof(val);
}