class optional
{
uint_of_size<bool> engaged_;
raw_storage_of_size<T> value_;
};
explicit optional<optional<T>>::operator bool() const {
return !(engaged_ & 0x10);
}
optional<T> const& optional<optional<T>>::operator*() const {
return reinterpret_cast<optional<T> const&>(*this);
}
template <typename T>
struct optional_optimized_disengaged_state_traits
{
constexpr static bool is_optimized = false;
constexpr static raw_storage_of_size<T> const& value();
};
template <typename T>
struct optional_optimized_disengaged_state_traits<T*>
{
constexpr static bool is_optimized = true;
constexpr static raw_storage_of_size<T> const& value() {
return cast( (T*)1 );
}
};
template <>
struct optional_optimized_disengaged_state_traits<bool>
{
constexpr static bool is_optimized = true;
constexpr static raw_storage_of_size<bool> const& value() {
return cast( 0x10 );
}
};
explicit optional<T>::operator bool() const {
return memcmp (&value_, &optional_optimized_disengaged_state_traits<T>::value(), sizeof(value_));
}
I can think of two ways to optimize the storage size for tr2::optional for certain T's -- at the cost of potentially adding run-time overhead when checking for the disengaged state:
For such types optional doesn't need to use a separate flag, but use the special value to indicate a disengaged state. In order to allow programmers to easily customize optional for their types, another interface could be standardized: a type trait:
template <typename T>
struct optional_optimized_disengaged_state_traits<T*>
{
constexpr static bool is_optimized = true;
constexpr static raw_storage_of_size<T> const& value() {
return cast( (T*)1 );
}
};
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
On quarta-feira, 2 de outubro de 2013 11:57:48, Andrzej Krzemieński wrote:
> template <typename T>
> struct optional_optimized_disengaged_state_traits<T*>
> {
> constexpr static bool is_optimized = true;
> constexpr static raw_storage_of_size<T> const& value() {
> return cast( (T*)1 );
> }
> };
You cannot assume that a pointer of value 1 is invalid. In fact, there's only
one pointer whose value is invalid and that's null (and such a pointer doesn't
have to be stored as a bitwise zero).
On 2 October 2013 13:57, Andrzej Krzemieński <akrz...@gmail.com> wrote:I can think of two ways to optimize the storage size for tr2::optional for certain T's -- at the cost of potentially adding run-time overhead when checking for the disengaged state:Is this anything more than an implementation detail (and therefore not on topic for this forum)?
I have yet to see *any* benchmarks of real world programs where adding such complexity would be a net benefit to anyone, let alone most users.If people really wish to go down this route, I can't stop them, other than to say that in all likelihood I will argue and vote strongly against such a proposal.
--