为什么std :: initializer_list不是内置的语言? [英] Why isn't std::initializer_list a language built-in?

查看:154
本文介绍了为什么std :: initializer_list不是内置的语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这是C ++ 11的一个重要特性,但它没有自己的保留关键字(或类似的东西)。

It seems to me that it's quite an important feature of C++11 and yet it doesn't have its own reserved keyword (or something alike).

而是 initializer_list 只是 来自标准库的模板类具有来自新的 braced-init-

Instead, initializer_list it's just a template class from the standard library that has a special, implicit mapping from the new braced-init-list {...} syntax that's handled by the compiler.

初始设想,这是一个由编译器处理的

At first thought, this solution is quite hacky.

这是C ++语言新增内容的实现方式: implicit roles 核心语言?

Is this the way new additions to the C++ language will be now implemented: by implicit roles of some template classes and not by the core language?

请考虑这些示例:

   widget<int> w = {1,2,3}; //this is how we want to use a class

为什么选择一个新类:

   widget( std::initializer_list<T> init )

,而不是使用类似这些提示:

instead of using something similar to any of these ideas:

   widget( T[] init, int length )  // (1)
   widget( T... init )             // (2)
   widget( std::vector<T> init )   // (3)




  1. code> const 这里和那里

  2. 语言中已经存在三个点(var-args,现在是可变参数模板),为什么不重用只要一个现有的容器,可以添加 const 的语法(并使其感觉到内置

  3. < &
  1. a classic array, you could probably add const here and there
  2. three dots already exist in the language (var-args, now variadic templates), why not re-use the syntax (and make it feel built-in)
  3. just an existing container, could add const and &

所有这些都已经是语言的一部分。我只写了我的3个第一个想法,我相信有很多其他方法。

All of them are already a part of the language. I only wrote my 3 first ideas, I am sure that there are many other approaches.

推荐答案

已经存在在 std 命名空间中定义的返回类型的核心语言特征的示例。 typeid 返回 std :: type_info 和(可能扩展一点) sizeof 返回 std :: size_t

There were already examples of "core" language features that returned types defined in the std namespace. typeid returns std::type_info and (stretching a point perhaps) sizeof returns std::size_t.

在前一种情况下,以便使用这个所谓的核心语言特性。

In the former case, you already need to include a standard header in order to use this so-called "core language" feature.

现在,对于初始化器列表,发生没有关键字需要生成对象,语法是上下文敏感花括号。除此之外,它与 type_info 相同。就个人而言,我不认为关键字的缺少使它更黑客。更奇怪的是,也许,但记住,目标是允许相同的支持初始化语法,已经允许的聚合。

Now, for initializer lists it happens that no keyword is needed to generate the object, the syntax is context-sensitive curly braces. Aside from that it's the same as type_info. Personally I don't think the absence of a keyword makes it "more hacky". Slightly more surprising, perhaps, but remember that the objective was to allow the same braced-initializer syntax that was already allowed for aggregates.

是的,你可能期望更多


  • 如果更多的情况出现,可以在没有新关键字的情况下引入新功能,那么委员会将采用它们

  • 如果新功能需要复杂类型,那么这些类型将放置在 std 中,而不是内置。

  • if more occasions arise where it is possible to introduce new features without new keywords then the committee will take them.
  • if new features require complex types, then those types will be placed in std rather than as builtins.

因此:


  • 如果新功能需要复杂类型并且可以没有新的关键字,那么你会得到你在这里,这是核心语言语法没有新的关键字,并使用从 std 。 / li>
  • if a new feature requires a complex type and can be introduced without new keywords then you'll get what you have here, which is "core language" syntax with no new keywords and that uses library types from std.

我认为,在C ++中核心语言和标准库之间没有绝对的划分。

What it comes down to, I think, is that there is no absolute division in C++ between the "core language" and the standard libraries. They're different chapters in the standard but each references the other, and it has always been so.

在C ++ 11中有另一种方法,也就是lambdas引入具有由编译器生成的匿名类型的对象。因为他们没有名字,他们根本不在命名空间,当然不在 std 。这不是一个合适的方法初始化列表,但是,当你写入接受一个构造函数时使用类型名称。

There is another approach in C++11, which is that lambdas introduce objects that have anonymous types generated by the compiler. Because they have no names they aren't in a namespace at all, certainly not in std. That's not a suitable approach for initializer lists, though, because you use the type name when you write the constructor that accepts one.

这篇关于为什么std :: initializer_list不是内置的语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆