Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
SGTech87 opened this issue May 28, 2024 · 0 comments
Open

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

SGTech87 opened this issue May 28, 2024 · 0 comments

Comments

@SGTech87
Copy link

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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant