Initialize tuple of references with reference to tuple

隐身守侯 提交于 2019-12-04 10:36:04

Github draft from 2014-07-23, [tuple.cnstr]

template <class... UType> constexpr tuple(tuple<UTypes...>&& u);

18Requires: sizeof...(Types) == sizeof...(UTypes). is_constructible<Ti, Ui&&>::value is true for all i.

20Remark: This constructor shall not participate in overload resolution unless each type in UTypes is implicitly convertible to its corresponding type in Types.

The Remarks: section defines the SFINAE. Note how it's different from the Requires: section by requiring the use of is_convertible instead of is_constructible.

In the OP's example, this leads to the check is_convertible<float, float&>, which is false: a float xvalue cannot be bound to a float lvalue reference:

is_convertible [meta.rel]/4

Given the following function prototype:

template <class T>
add_rvalue_reference_t<T>::type create() noexcept;

the predicate condition for a template specialization is_convertible<From, To> shall be satisfied if and only if the following vode would be well-formed, including any implicit conversions to the return type of the function:

To test() {
    return create<From>();
}

Here,

float& test() {
    return create<float>();
}

is ill-formed, create<float>() returns a float&&, that is, an xvalue. The result cannot be bound to an lvalue-reference.


It is well-known that the construction of tuple is not perfect; see for example proposal N3680, which also addresses LWG defect 2051.

None of those seem to address the issue in the OP, however.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!