- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
首先,我有一个结构,它有一个值和一个默认值
struct S {
int a = 1;
};
当 gcc 和 clang 都是 non-const/non-constexpr 时,可以默认构造此类型。在两者下,std::is_pod<S>::value
是 false
.奇怪的行为如下:
S s1; // works under both
const S s2{}; // works under both
const S s3; // only works in gcc, clang wants a user-provided constructor
以下尝试都不会影响 clang:
struct S {
int a = 1;
constexpr S() = default; // defaulted ctor
virtual void f() { } // virtual function, not an aggregate
private:
int b = 2; // private member, really not an aggregate
};
我唯一能做的就是添加 constexpr S() { }
明确地。 const S s;
对我来说似乎真的错了失败,而 const S s{};
尤其是当类型不是聚合时。
标准让我觉得Clang是对的
N4296 : 8.5/7
If a program calls for the default initialization of an object of a const-qualified type T, T shall be a class type with a user-provided default constructor
那么为什么 gcc 允许这样做,并且是 S{};
不是默认初始化,即使类型不是 POD 或聚合?
最佳答案
const S s3;
被 [dcl.init]/12 覆盖:
If no initializer is specified for an object, the object is default-initialized.
因此,根据您的报价要求,必须存在用户提供的默认构造函数。像这样添加一个
struct S {
int a = 1;
constexpr S(){}
};
然后 makes the declaration compile fine .
[..] especially when the type is not an aggregate.
S
在您的情况下是一个聚合,以及 const S s{}
有效的原因。 const S s{}
应用聚合初始化,一切正常。
如果 S
不是聚合,则
List-initialization of an object or reference of type T is defined as follows:
- If
T
is an aggregate, aggregate initialization is performed (8.5.1).- Otherwise, if the initializer list has no elements and
T
is a class type with a default constructor, the object is value-initialized.
现在考虑值初始化的定义:
To value-initialize an object of type
T
means:
- if
T
is a (possibly cv-qualified) class type (Clause 9) with either no default constructor (12.1) or a default constructor that is user-provided or deleted, then the object is default-initialized;- if
T
is a (possibly cv-qualified) class type without a user-provided or deleted default constructor, then the object is zero-initialized and the semantic constraints for default-initialization are checked, and ifT
has a non-trivial default constructor, the object is default-initialized;
默认 ctor 确实很重要,因为成员有一个初始化程序 ([class.ctor]/4.9),但这无关紧要,因为无论如何都要检查约束。因此它是默认初始化,并且行
const S s{};
与
一样有效(或无效!)const S t;
So why does gcc allow this
嗯:
就目前的标准而言,GCC 是不合规的;见上文。
有一个活跃的 CWG 问题,#253 ,创建于十五年前,涵盖了类似的场景。 2011 年 session 上关于这个的最后说明说
If the implicit default constructor initializes all subobjects, no initializer should be required.
S
的隐式默认构造函数就是这种情况,这将使你的所有行都有效。
GCC 开发者(例如 here)暗示由于委员会基本同意上述决议,GCC 的当前行为是可行的,不应调整。因此,人们很可能会争辩说 GCC 是正确的,并且标准被打破了。
关于c++ - 常量 T{};作品,常量T;当 T 是非 POD 时失败,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29683381/
假设你有两个类A和B。如果它们之间的关系是has-a 即A 有一个 B 如何将信息从 B 传递到 A?例如,假设您在 B 中进行了计算并需要 A 中的答案。 除了将 A 类的指针传递给 B 类并调用一
我正在尝试创建一个随机用户名生成器,当用户单击“gen-button”时,它应该会为他们提供一个随机用户名。我是 javascript 的新手,所以不胜感激:) var nameList = [
我知道这不是一个支持论坛;我正试图将其作为文档请求偷偷放入。 简短的问题:创建在所有方面都与原生元素完全相同的元素的推荐方法是什么? (提供附加功能,是的,但是是 100% 兼容的直接替换原生的) 长
面临一个问题 - 是否真的可以将动态选择属性传递给 FindAll 方法?它不起作用,因为它没有得到认可。 selectAttributes= "'x_name','y_name','z_name',
我有以下代码: function example(){ executing_code; $(function(){ executing_code; }); (function(){ executing
将 apache 版本(5.6.12)升级到最新版本后,mysql 在导入转储文件时返回重复错误。 简单的问题是如何导出适用于所有php mysql和mysql的mysql转储文件。 将文件导入到:服
这个 js fiddle 运行没有错误或问题: http://jsfiddle.net/3j45B/2/ 但是,当嵌入到 http://www.zabb.co.uk/untitled6.html 中时
我有一个脚本可以更新数据库中的日期字段。 (已购买)。 我还在另一部分中使用该数据更新第二个日期字段,该字段采用输入日期并添加 6 年 $duedate。它工作正常,但我收到变量 purchased
这个问题在这里已经有了答案: Logical Operators in C (8 个答案) 关闭 9 年前。 main( ) { int i = 4, j = -1, k = 0, w,
我从 http://en.wikipedia.org/wiki/NOP 中红色定义但我仍然需要一个更简单的定义。 我偶然发现了一段代码,但我不知道它到底做了什么: switch (Something)
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 2年前关闭。 Improve thi
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 10 年前。 Improve thi
嘿伙计们,我是一个新人,试图找出一些令人困惑的方法 var delay = (function(){ var timer = 0; return function(ca
我已经根据 MediaRecorder.isTypeSupported 对此进行了研究 Chromium 不支持音频/ogg;编解码器=作品。有谁知道是否有与 MediaRecorder 一起使用的替
我已经根据 MediaRecorder.isTypeSupported 对此进行了研究 Chromium 不支持音频/ogg;编解码器=作品。有谁知道是否有与 MediaRecorder 一起使用的替
我目前正在开发一个 VOIP 应用程序。为此,我使用 PortAudio 库检索和播放声音,并使用 Opus 库编码和解码声音数据包。 目前,我成功地使用了 PortAudio。我的程序只是做: 从麦
当我运行此代码时,它会抛出一个错误,指出“clear”未被识别为内部或外部命令。 #include #include void main() { int a=20; printf("print a n
我想知道下面这行代码是如何工作的? char c = (char) -98; 据我所知,所有有符号数都以2 的补码 形式存储。所以 -98 将以 2 的补码形式存储。因此,如果您键入将其转换为 cha
我在我的 OSX 应用程序中发现了一个问题,我认为这是 Mavericks 的错误。 我有以下层次结构: NSView->NSScrollView->NSClipView->NSTableView 当
这是我驱动器上的地址: C:\Program Files\Java\jdk1.6.0_18\bin 我将如何设置路径变量,以便我可以进入命令窗口 (windowskey+r "cmd") 并能够键入如
我是一名优秀的程序员,十分优秀!