Skip to content

Why is the ctor of optional<T&> explicit? #99

Open

Description

@ImplOfAnImpl

I mean the ctor template <class U> explicit optional(const optional<U&>& rhs). It doesn't seem to have much sense - optional<T&> is logically a reference and it seems reasonable to allow it to be converted implicitly if the underlying pointer types allow this.
With the current implementation the usage of optional references can be cumbersome, because even the non-const-ref to const-ref conversion has to be explicit.

Activity

  1. akrzemi1 commented on Nov 24, 2021

    @akrzemi1
    Member

    This is for consistency with the non-reference specialization: optional<U> is not implicitly convertible to optional<T> even if U is convertible to T.

    The missing T-to-const T can indeed be disturbing. This is also the case for non-references:

    boost::optional<int> oi;
    boost::optional<const int> oci = oi;

    We could consider special casing the T-to-const T conversion.

  2. ImplOfAnImpl commented on Nov 24, 2021

    @ImplOfAnImpl
    Author

    I see. But does this consistency actually make sense? I mean, for value types the implicit conversion can indeed do nasty stuff like slicing or narrowing, but converting references seems to always be benign.

    Btw, both kinds of optional have assignment operators corresponding to explicit ctors, which kind of defeats the purpose, because you can still perform an implicit conversion:

    boost::optional<float> of;
    boost::optional<int> oi;
    oi = of;
    

    So it might make sense to make boost::optional<T> behave like std::optional in this regard, i.e. the converting ctor is implicit if the underlying types are implicitly convertible to each other (and the assignment is present only of the underlying types are assignable). In this case making ctors of boost::optional<T&> implicit will actually make it consistent with boost::optional<T>.

  3. akrzemi1 commented on Nov 24, 2021

    @akrzemi1
    Member

    Unless the binding of temporaries to lvalue references to const is explicitly disabled., even the reference casts have the same problems:

      int i = -1;
      unsigned const& ui = i;

    Consistency with std::optional is not necessarily a good thing, as std::optional has design flaws that cause ambiguities:

    template <typename T, typename U>
    void test_optional()
    {
      std::optional<U> ou = std::nullopt;
      std::optional<T> ot = ou;
      assert (ot == std::nullopt); // true for some Ts and Us, false for other Ts and Us.
    }

    The above looks like an obvious thing to expect, but fails for

    test_optional<std::optional<int>, int>();

    boost::optional has fewer gotchas like the one above, because it does not try to be 100% consistent with std::optional.

  4. ImplOfAnImpl commented on Nov 24, 2021

    @ImplOfAnImpl
    Author

    Unless the binding of temporaries to lvalue references to const is explicitly disabled. Even the reference casts have the same problems

    Yes, I meant converting lvalue references of course (there is no boost::optional<T&&> anyway).

    Consistency with std::optional is not necessarily a good thing

    I see.

  5. akrzemi1 commented on Nov 24, 2021

    @akrzemi1
    Member

    Would allowing a conversion from T to const T address your issue?
    Or conversion from Derived& to Base&?

  6. ImplOfAnImpl commented on Nov 24, 2021

    @ImplOfAnImpl
    Author

    Well, the absence of the T& to const T& conversion is what caused me to report the issue.
    But IMO the conversion from Derived& to Base& would also be nice to have.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions