- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
此标准草案显示了 initializer_list
的概要.它没有私有(private)构造函数。
但是我看过的两个标准库实现,libstdc++ 和 libc++,都提供私有(private)构造函数:
// The compiler can call a private constructor.
constexpr initializer_list(const_iterator __a, size_type __l)
: _M_array(__a), _M_len(__l) { }
_LIBCPP_ALWAYS_INLINE
_LIBCPP_CONSTEXPR_AFTER_CXX11
initializer_list(const _Ep* __b, size_t __s) _NOEXCEPT
: __begin_(__b),
__size_(__s)
{}
我相信这个私有(private)构造函数是“隐含”的部分源于§8.5.4/5:
An object of type
std::initializer_list<E>
is constructed from an initializer list as if the implementation allocated a temporary array of N elements of typeconst E
, where N is the number of elements in the initializer list. Each element of that array is copy-initialized with the corresponding element of the initializer list, and thestd::initializer_list<E>
object is constructed to refer to that array.
所以我的问题是:
大纲是否未详细说明?
库是否需要私有(private)构造函数?它做了哪些编译器做不到的事情?
最佳答案
Is the synopsis under-specified?
不,它记录了 initializer_list
的面向用户的位类模板,您实际上可以在代码中使用的部分。根据概要,模板仅包含一个默认构造函数,允许您创建空 initializer_list
s,这显然不是很有用。然而,initializer_list<T>
是一种依赖于编译器执行的某些魔法 的类型。神奇的是,我指的是您引用的§8.5.4/5。这使得下面的语句是合法的并且可以编译。
std::initializer_list<int> numbers{1, 2, 3, 4}; // no such constructor in the synopsis
这里,如 §8.5.4/5 中所述,编译器将创建一个包含四个整数的数组,然后初始化 initializer_list<int>
带有一对指针(第一个元素和结束元素之后的一个)或一个指针和长度(libstdc++ 和 libc++ 似乎都这样做)的实例。
创建实例后,您的代码就可以访问概要中列出的所有成员函数。
Does the library need a private constructor? What does it do that the compiler can't?
正如 libstdc++ 私有(private)构造函数定义上方的注释所暗示的那样,编译器能够发出绕过正常访问控制的代码,所以不,我认为拥有该构造函数不是必需的。编译器可以使用默认构造函数构造一个空的 initializer_list
实例,然后将适当的值分配给私有(private)数据成员(这些也没有在概要中列出,但是是必需的)。
但是,当私有(private)构造函数提供编译器可以调用的干净接口(interface)时,为什么还要为这种笨拙而烦恼呢?
关于c++ - 是否为 initializer_list 提供一个私有(private)构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29200036/
据我所知,在 C# 中,所有字段默认都是私有(private)的,如果没有另外标记的话。 class Foo { private string bar; } class Foo { strin
这个问题在这里已经有了答案: Why is it allowed to access Java private fields via reflection? (7 个答案) 关闭 6 年前。 使用反
在 C#(和许多其他语言)中,访问相同类型的其他实例的私有(private)字段是完全合法的。例如: public class Foo { private bool aBool; pu
使用私有(private)方法通过将一些决策点重构为单独的方法来降低 CC 会降低实际方法的 CC 并易于阅读,但不会减少在测试中获得完整分支覆盖的工作量。 这合理吗?你有什么现场经验? 最佳答案 好
在下面的例子中,模块outer有一个私有(private)类型Private和一个私有(private)内部模块inner。 inner 能够访问Private(因为子模块可以访问其父级的私有(pri
class Person def one @var = 99 self.two end private def two p @var end end p=P
我在 Azure 中创建了 VNET。我放入了一个子集 Azure Private Link,它在 VNET 之外和另一台虚拟机中调用 Azure Function。 当我尝试通过专用 IP 调用专用
我在 Azure 中创建了 VNET。我放入了一个子集 Azure Private Link,它在 VNET 之外和另一台虚拟机中调用 Azure Function。 当我尝试通过专用 IP 调用专用
我正在尝试获得良好的 Ruby 编码风格。为防止意外调用具有相同名称的局部变量,我总是在适当的地方使用 self.。但是现在我偶然发现了这个: class MyClass "method" a
今天遇到一个案例类构造函数的奇怪问题。我想将构造函数设为私有(private),看来这不是问题。所以我已经在我的一个项目中尝试过它并且它有效。但在另一个项目中,我可以调用私有(private)构造函数
我想坚持使用记录,并且不想返回对象。所以我想知道是否可以将记录的字段设置为私有(private)?或者创建记录的私有(private)成员。其他具体类型(例如可区分联合)怎么样? 或者,这个要求是否违
我正在通过 Flickr API 进行经过身份验证的调用来访问照片。但我只得到我的公开照片,而没有任何私有(private)照片。 下面给出的是我正在使用的代码, Flickr f; Request
这两个类的行为不同;原因似乎与使用 private[this] 声明而不是 private 有关。有人可以解释一下为什么吗? 私有(private): class Person( private
在 private 中的 1000 秒 private 之后,我想到可能不需要它 public class Outer { private static class Inner { // yo
我有以下代码: class C { private enum E { // ... } } private extension C { func f(e: E)
OOP 语言中是否有object-private 的概念??我的意思是比经典的私有(private)访问限制更多? Private (or class-private) restricts the a
swift 3.0 我知道fileprivate访问级别修饰符将函数/属性的使用限制在声明它的源文件和 private - 仅限于声明的词法范围。但似乎这条规则不适用于扩展。例如。此代码有效: cla
即将推出的 Delphi 版本中启用该功能的功能怎么样? 也许它可能是一个编译器开关,促进所有 ** private **s to ** strict private **小号。 ... 或者它可能是
我可以通过将函数放入类的私有(private)扩展中来创建私有(private)函数,而不是通过不断调用 private func functionName(){} 来创建新的私有(private)函
部署专用端点并需要专用 IP 地址作为输出,但似乎无法正确获取值查询。下面的结果是“模板输出'主机名'无效:语言表达式属性|'privateIPAddress'具有无效的数组索引..(代码:Deplo
我是一名优秀的程序员,十分优秀!