[英]std::initializer_list<int const> cannot be constructed from std::initializer_list<int>
My question is about the lack of conversions between std::initializer_list
types where the contained type is more or less cv qualified when those conversions appear to be easily doable. 我的问题是
std::initializer_list
类型之间缺少转换,其中包含的类型或多或少cv合格,而这些转换似乎很容易实现。
Consider the following code which is invalid: 请考虑以下无效的代码:
std::initializer_list<int> x{1,2,3,4,5,6};
std::initializer_list<int const> y = x; // error! cannot convert!
Now consider std::optional
and ignore the ridiculous types: 现在考虑
std::optional
并忽略荒谬的类型:
std::optional<std::vector<int>> x{std::in_place, {1,2,3,4,5}}; // OK
std::optional<std::vector<int const>> y{std::in_place, {1,2,3,4,5}}; // error!
I assume the language spec requires deducing non cv qualified U
's for std::initializer_list<U>
by default. 我假设语言规范要求默认情况下为
std::initializer_list<U>
推导非cv限定的U
'。
As far as I can tell, the whole point of std::optional
(and std::any
and std::variant
) having std::initializer_list
constructor overloads is to avoid specifying the exact type of the initializer list. 据我所知,具有
std::initializer_list
构造函数重载的std::optional
(和std::any
和std::variant
)的重点是避免指定初始化列表的确切类型。 To get the second line of the above code to compile, that's exactly what you must do. 要获得上述代码的第二行进行编译,这正是您必须要做的。
std::initializer_list
already holds a const*
to it's data (in libc++
anyhow). std::initializer_list
已经为它的数据保存了一个const*
(无论如何在libc++
)。 There's no reason I can see for the above code not to work? 我没有理由看到上面的代码无法工作? Is this a solvable issue in the language, or am I missing something?
这是语言中可解决的问题,还是我错过了什么?
It sounds like your question is why std::initializer_list<T const>
cannot be constructed from std::initializer_list<T>
despite the fact that it would be easy to implement such a conversion. 听起来你的问题是为什么
std::initializer_list<T const>
不能从std::initializer_list<T>
构造,尽管事实上很容易实现这样的转换。
I think the answer is that you are not supposed to have std::initializer_list<T const>
in the first place, given that, as you noted, std::initializer_list<T>
only gives const
access to its elements anyway. 我认为答案是你不应该首先使用
std::initializer_list<T const>
,因为正如你所指出的那样, std::initializer_list<T>
无论如何只允许const
访问它的元素。
It might therefore be said that there is a "cultural norm" in C++ that you are not supposed to have any constructors that require a std::initializer_list<T const>
argument. 因此可以说C ++中存在一个“文化规范”,你不应该有任何需要
std::initializer_list<T const>
参数的std::initializer_list<T const>
函数。 None of the standard library containers do, for instance, since a cv-qualified value type is illegal anyway (except in the case of std::array
, which, of course, has no user-defined constructors anyway). 例如,没有标准库容器可以执行,因为cv限定的值类型无论如何都是非法的(除了
std::array
,当然,无论如何都没有用户定义的构造函数)。
If you write your own type MyContainer
that supports MyContainer<T const>
, then I suggest that you make it look like the following: 如果您编写自己的
MyContainer
类型支持MyContainer<T const>
,那么我建议您使其如下所示:
template <class T>
class MyContainer {
public:
MyContainer(std::initializer_list<std::remove_cv_t<T>> il);
// ...
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.