Doc. no.: P0608R3 Date: 2018-10-03 Audience: LWG Reply-to: Zhihao Yuan <zy at miator dot net>

A sane variant converting constructor

Changes since R1

Discuss whether to add special cases for unambiguous conversions.

Take the user-defined types that trigger standard conversions into account.

Remove an unhelpful example.

Problems to solve

variant constructs entirely unintended alternatives.

variant< string , bool > x = "abc" ;

The above holds string with the proposed fix.

variant prefers constructions with information losses.

variant<char, optional<char16_t>> x = u'\u2043' ; // holds char = 'C' double d = 3.14 ; variant<int, reference_wrapper<double>> y = d; // holds int = 3

The above preserves the input value in optional<char16_t> and reference_wrapper<double> , respectively, with the proposed fix.

variant performs unstable constructions.

using T = variant< float , int >; T v; v = 0 ;

When T is upgraded to variant<float, long> ,

using T = variant< float , long >; T v; v = 0 ;

When T is upgraded to variant<float, big_int<256>> ,

using T = variant< float , big_int< 256 >>; T v; v = 0 ;

In both cases, the proposed fix consistently constructs with the second alternative.

As shown, the problems equally apply to the converting constructor and the converting assignment operator.

See also LEWG 227.

Proposed resolution

This paper proposes to constrain the variant converting constructor and the converting assignment operator to prevent narrowing conversions and conversions to bool . This section explains what exactly this change brings.

Lemma 1. Let X X be a sum type T+U T + U . Let A={T1,T2,T3,...} A = { T 1 , T 2 , T 3 , . . . } be a set of types that are convertible to T T , and B={U1,U2,U3,...} B = { U 1 , U 2 , U 3 , . . . } be a set of types that are convertible to U U . Let Y Y be a set of types that are convertible to X X . Y=A⊖B Y = A ⊖ B (symmetric difference) rather than A∪B A ∪ B , because each Ti=Uj T i = U j causes an ambiguity.

Theorem 1. If A A and B B are extended to include a type τ∈A∩B τ ∈ A ∩ B , Y Y is shrunk.

In short, constraining variant< Ts... T s . . . > (with Ts¯¯¯¯¯¯>1 T s ¯ > 1 ) converting constructor may enable more types to be convertible to a variant.

Definition 1. For type T T that is convertible to T′ T ′ , let P P be a set of all the possible values for T T , and Q Q be a set of all the possible values for T′ T ′ . If P⊆Q P ⊆ Q , T′ T ′ is denoted as T+ T + . Otherwise, P⊈Q P ⊈ Q and T′ T ′ is denoted as T− T − . The conversion from T T to T− T − (denoted as T⇀T− T ⇀ T − ) is a potentially unrepresentable conversion.

In this paper, narrowing conversions (considering only the types) and boolean conversions assemble the potentially unrepresentable conversions in C++.

Lemma 2. Potentially unrepresentable conversions in C++ have Conversion rank.

The proof is left as an exercise for the reader.

Let X be variant< Ts... T s . . . > , r be a value of T T .

When Ts¯¯¯¯¯¯=1 T s ¯ = 1 , without loss of generality, X is variant< T− T − > . Effects of the proposed resolution can be summarized as follows:

X v = r; before after variant<float> v = 0; holds .0f ill-formed variant<float> v = INT_MAX; holds INT_MAX + 1 ill-formed

However, variant<V> is such a rare variant, as you can hardly say that V+⊥ V + ⊥ is a sum type.

When Ts¯¯¯¯¯¯=2 T s ¯ = 2 , X is variant< T−,U T − , U > .

If T T is not convertible to U U , the effects are the same as the case T s ¯ ¯ ¯ ¯ ¯ ¯ = 1 T s ¯ = 1 .

is not convertible to , the effects are the same as the case . If T ⇀ U T ⇀ U is an Exact Match or a Promotion, T ⇀ T − T ⇀ T − has a worse conversion sequence, U U is constructed in X v = r under the existing rule. The proposed resolution constructs U U as well because T ⇀ T − T ⇀ T − is not viable.

is an Exact Match or a Promotion, has a worse conversion sequence, is constructed in under the existing rule. The proposed resolution constructs as well because is not viable. If T T is potentially unrepresentable converted to U U , according to Lemma 2, both T ⇀ U T ⇀ U and T ⇀ T − T ⇀ T − have Conversion rank, so X v = r is ill-formed due to ambiguity under the existing rule. Meanwhile, it’s also ill-formed given the proposed resolution, because both conversions are potentially unrepresentable.

is potentially unrepresentable converted to , according to Lemma 2, both and have Conversion rank, so is ill-formed due to ambiguity under the existing rule. Meanwhile, it’s also ill-formed given the proposed resolution, because both conversions are potentially unrepresentable. If T ⇀ U T ⇀ U has Conversion rank and is not potentially unrepresentable, X v = r is still ill-formed under the existing rule. However, U U is constructed given the proposed resolution.

has Conversion rank and is not potentially unrepresentable, is still ill-formed under the existing rule. However, is constructed given the proposed resolution. If T ⇀ U T ⇀ U is a user-defined conversion, X v = r constructs T − T − under the existing rule. With the proposed resolution, it constructs U U instead.

The effects are summarized in the order of the bullets:

X v = r; before after variant<float, vector<int>> v = 0; holds float ill-formed variant<float, int> v = 'a'; holds int('a') holds int('a') variant<float, char> v = 0; ill-formed ill-formed variant<float, long> v = 0; ill-formed holds long variant<float, big_int<256>> v = 0; holds float holds big_int

When Ts¯¯¯¯¯¯>2 T s ¯ > 2 , let Ts1=T− T s 1 = T − , S S be an overload set {f(τ)∣τ∈Ts} { f ( τ ) ∣ τ ∈ T s } , S′=S−{f(T−)} S ′ = S − { f ( T − ) } .

If S ( r ) S ( r ) resolves to f ( T − ) f ( T − ) , this is equivalent to the case T s ¯ ¯ ¯ ¯ ¯ ¯ = 1 T s ¯ = 1 ; or

resolves to , this is equivalent to the case ; or if S ′ ( r ) S ′ ( r ) resolves to f ( U ) f ( U ) where T ⇀ U T ⇀ U has Conversion rank, X v = r is ill-formed without the proposed resolution; or

resolves to where has Conversion rank, is ill-formed without the proposed resolution; or if S ′ ( r ) S ′ ( r ) resolves to f ( U ) f ( U ) where T ⇀ U T ⇀ U is a user-defined conversion, S ′ − { f ( U ) } S ′ − { f ( U ) } has no viable function against r r , and the situation is equivalent to the last bullet in the case T s ¯ ¯ ¯ ¯ ¯ ¯ = 2 T s ¯ = 2 .

resolves to where is a user-defined conversion, has no viable function against , and the situation is equivalent to the last bullet in the case . Otherwise, S ′ ( r ) S ′ ( r ) resolves to f ( U ) f ( U ) where T ⇀ U T ⇀ U is an Exact Match or a Promotion, X v = r constructs U U with or without the proposed resolution.

The effects are summarized in the order of the bullets:

X v = r; before after variant<float, big_int<256>, big_int<128>> v = 0; holds float ill-formed variant<float, long, double> v = 0; ill-formed holds long variant<float, vector<int>, big_int<256>> v = 0; holds float holds big_int variant<float, int, big_int<256>> v = 'a'; holds int holds int

Theorem 2. For variant< Ts... T s . . . > v = r , where r is a value of R R , when there exists one and only one τ∈Ts τ ∈ T s rendering R R to be potentially unrepresentable converted to τ τ , the proposed resolution may cause breaking changes

by preventing the initialization if it used to construct τ τ , or

, or by preferring a different and user-defined conversion R ⇀ U R ⇀ U when there is exactly one U ∈ T s − { τ } U ∈ T s − { τ } that is convertible from R R .

The first case is easy to fix while the second gives the desired outcome for this paper. Both behaviors to be changed are bugs, not features, as shown in Section 1.

However, the definition of potentially unrepresentable conversions here makes it unable to be fully detected in the library, because while we understand type conversions as mappings, C++ type conversions happen in conversion sequences. A boolean conversion in a standard conversion sequence that follows a user-defined conversion sequence cannot be detected. In short, we cannot distinguish

struct Bad { operator char const *(); };

from

struct LessBad { operator bool () ; };

The proposed resolution considers both to be bad. Throughout the library, only two types are affected: true_type and false_type . The most commonly seen cases, types that are contextually converted to bool , are not affected by this compromise, because std::variant is not contextual. The compromise also does not break our proof, because user-defined conversions’ rank is lower than that of narrowing conversions.

Alternative designs

The author came up with and experimented a few other designs, here we list two basic ideas.

Use the alternatives’ order information in determining which one to construct. The idea defeats the purpose of the converting constructor because if the construction is sensitive to the order of the alternatives declared in the variant template argument list, in_place_index would be a better choice. The converting constructor and assignment operator assume unordered alternatives. Distinguish implicit and explicit conversions. First, the idea doesn’t work well with the converting assignment operator; applying the implicit policy seems to be the only choice to maintain a consistent behavior, but this may be overkill. Second, it is counterintuitive to have an explicit constructor accepting fewer types comparing to an implicit one because of Theorem 1.

An additional suggestion is to allow narrowing but unambiguous conversions. In the following example, if double_double is a user-defined type that does not convert from an integer type,

variant< char , double_double> v = 42 ;

, it might be not so problematic to construct the char alternative. However, special casing this makes the construction less stable. If double_double is upgraded to support converting from int , the code silently constructs the double_double alternative.

Wording

This wording is relative to N4762.

Modify 19.7.3.1 [variant.ctor]/12 as indicated:

template<class T> constexpr variant(T&& t) noexcept(see below );

Let T j be a type that is determined as follows: build an imaginary function FUN (T i ) for each alternative type T i for which T i x[] = {std::forward<T>(t)}; is well-formed for some invented variable x and, if T i is cv bool , remove_cvref_t<T> is bool . The overload FUN (T i ) selected by overload resolution for the expression FUN (std::forward<T>(t)) defines the alternative T j which is the type of the contained value after construction.

[…]

[Drafting note: The above uses T i x[] = {std::forward<T>(t)}; rather than T i {std::forward<T>(t)} to test whether the conversion sequence from T to T i contains a narrowing conversion because whether T i {std::forward<T>(t)} is well-formed may subject to whether T i has an initializer_list constructor or whether T i is an aggregate. Here is an example: https://godbolt.org/z/Ck5w-L –end note]

Modify 19.7.3.3 [variant.assign]/10 as indicated:

template<class T> variant& operator=(T&& t) noexcept(see below );

Let T j be a type that is determined as follows: build an imaginary function FUN (T i ) for each alternative type T i for which T i x[] = {std::forward<T>(t)}; is well-formed for some invented variable x and, if T i is cv bool , remove_cvref_t<T> is bool . The overload FUN (T i ) selected by overload resolution for the expression FUN (std::forward<T>(t)) defines the alternative T j which is the type of the contained value after assignment.