This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of New status.

2811. "Selected constructor" wording is incorrect for optional/variant/any

Section: 22.5.3.2 [optional.ctor], 22.5.3.4 [optional.assign], 22.6.3.2 [variant.ctor], 22.7.4.2 [any.cons], 22.7.4.4 [any.modifiers] Status: New Submitter: Tim Song Opened: 2016-10-29 Last modified: 2020-06-13

Priority: 3

View all other issues in [optional.ctor].

View all issues with New status.

Discussion:

Throughout optional/variant/any's specification references are made to "the selected constructor of T". For example, 22.5.3.2 [optional.ctor]/16 says of the constructor from const T&:

-16- Remarks: If T's selected constructor is a constexpr constructor, this constructor shall be a constexpr constructor.

Similarly, the in-place constructor has this wording (22.5.3.2 [optional.ctor]/25-26):

-25- Throws: Any exception thrown by the selected constructor of T.

-26- Remarks: If T's constructor selected for the initialization is a constexpr constructor, this constructor shall be a constexpr constructor.

If T is a scalar type, it has no constructor at all. Moreover, even for class types, the in-place constructor wording ignores any implicit conversion done on the argument before the selected constructor is called, which 1) may not be valid in constant expressions and 2) may throw an exception; such exceptions aren't thrown "by the selected constructor of T" but outside it.

The wording should probably be recast to refer to the entire initialization.

[Issues Telecon 16-Dec-2016]

Priority 3; Jonathan to provide wording.

[2020-06-11; Nina Dinka Ranns comments and provides initial wording]

This wording depends on the current resolution for LWG 2833(i), which covers the constexpr portion of this issue.

Proposed resolution:

This wording is relative to N4861.

  1. Modify 22.5.3.2 [optional.ctor] as indicated:

    constexpr optional(const optional& rhs);
    

    -3- […]

    -4- […]

    -5- Throws: Any exception thrown by the selected constructor of T.

    constexpr optional(optional&& rhs) noexcept(see below);
    

    -7- […]

    […]

    -10- Throws: Any exception thrown by the selected constructor of T.

    template<class... Args> constexpr explicit optional(in_place_t, Args&&... args);
    

    -12- […]

    […]

    -15- Throws: Any exception thrown by the selected constructor of T.

    template<class U, class... Args>
      constexpr explicit optional(in_place_t, initializer_list<U> il, Args&&... args);
    

    -17- […]

    […]

    -20- Throws: Any exception thrown by the selected constructor of T.

    template<class U = T> constexpr explicit(see below) optional(U&& v);
    

    -22- […]

    […]

    -25- Throws: Any exception thrown by the selected constructor of T.

    template<class U> explicit(see below) optional(const optional<U>& rhs);
    

    -27- […]

    […]

    -30- Throws: Any exception thrown by the selected constructor of T.

    template<class U> explicit(see below) optional(optional<U>&& rhs);
    

    -32- […]

    […]

    -35- Throws: Any exception thrown by the selected constructor of T.

  2. Modify 22.6.3.2 [variant.ctor] as indicated:

    template<class T, class... Args> constexpr explicit variant(in_place_type_t<T>, Args&&... args);
    

    -20- […]

    […]

    -23- Throws: Any exception thrown by calling the selected constructor of T.

    template<class T, class U, class... Args>
      constexpr explicit variant(in_place_type_t<T>, initializer_list<U> il, Args&&... args);
    

    -25- […]

    […]

    -28- Throws: Any exception thrown by calling the selected constructor of T.

    template<size_t I, class... Args> constexpr explicit variant(in_place_index_t<I>, Args&&... args);
    

    -30 […]

    […]

    -33- Throws: Any exception thrown by calling the selected constructor of TI.

  3. Modify 22.7.4.2 [any.cons] as indicated:

    any(const any& other);
    

    -2- Effects: […]

    -3- Throws: Any exceptions arising from calling the selected constructor for the contained value.

    […]
    template<class T>
      any(T&& value);
    

    -5- […]

    […]

    -9- Throws: Any exception thrown by the selected constructor of VT.

    template<class T, class... Args>
      explicit any(in_place_type_t<T>, Args&&... args);
    

    -10- […]

    […]

    -15- Throws: Any exception thrown by the selected constructor of VT.

    template<class T, class U, class... Args>
      explicit any(in_place_type_t<T>, initializer_list<U> il, Args&&... args);
    

    -16- […]

    […]

    -21- Throws: Any exception thrown by the selected constructor of VT.

  4. Modify 22.7.4.4 [any.modifiers] as indicated:

    template<class T, class... Args>
      decay_t<T>& emplace(Args&&... args);
    

    -1- […]

    […]

    -7- Throws: Any exception thrown by the selected constructor of VT.

    -8- […]

    template<class T, class U, class... Args>
      decay_t<T>& emplace(initializer_list<U> il, Args&&... args);
    

    -9- […]

    […]

    -15- Throws: Any exception thrown by the selected constructor of VT.