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

2974. Diagnose out of bounds tuple_element/variant_alternative

Section: 22.3.4 [pair.astuple], 22.6.4 [variant.helper] Status: C++20 Submitter: Agustín K-ballo Bergé Opened: 2017-06-10 Last modified: 2021-02-25

Priority: Not Prioritized

View all other issues in [pair.astuple].

View all issues with C++20 status.

Discussion:

Instantiating tuple_element with an out-of-bounds index requires a diagnostic for tuple and array, but not for pair. The specification requires an out-of-bounds index for pair to go to the base template instead, which is undefined.

Similarly, instantiating variant_alternative with an out-of-bounds index violates a requirement, but it is not required to be diagnosed.

[2017-06-12, Moved to Tentatively Ready after 5 positive votes on c++std-lib]

Proposed resolution:

This wording is relative to N4659.

  1. Modify 22.2.1 [utility.syn], header <utility> synopsis, as indicated:

    […]
    
    // 22.3.4 [pair.astuple], tuple-like access to pair
    template <class T> class tuple_size;
    template <size_t I, class T> class tuple_element;
    template <class T1, class T2> struct tuple_size<pair<T1, T2>>;
    template <size_t I, class T1, class T2> struct tuple_element<I, pair<T1, T2>>;
    template <class T1, class T2> struct tuple_element<0, pair<T1, T2>>;
    template <class T1, class T2> struct tuple_element<1, pair<T1, T2>>;
    
    […]
    
  2. Modify 22.3.4 [pair.astuple] as indicated:

    tuple_element<0, pair<T1, T2>>::type
    

    -1- Value: The type T1.

    tuple_element<1, pair<T1, T2>>::type
    

    -2- Value: The type T2.

    tuple_element<I, pair<T1, T2>>::type
    

    -?- Requires: I < 2. The program is ill-formed if I is out of bounds.

    -?- Value: The type T1 if I == 0, otherwise the type T2.

  3. Modify 22.6.4 [variant.helper] as indicated:

    variant_alternative<I, variant<Types...>>::type
    

    -4- Requires: I < sizeof...(Types). The program is ill-formed if I is out of bounds.

    -5- Value: The type TI.