- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
此代码在 Coliru 中编译时带有警告 [unitialized members a[1].i
和 a[2].i
在std::cout <<
main()
中的表达式] 但在 Ideone 中正常编译.
#include <iostream>
struct A
{
int i;
A(int j) : i{j} {};
A() = default;
};
int main() {
A a[3] = { A(1) };
std::cout << a[1].i << ' ' << a[2].i << '\n';
}
根据我对 iso § 8.5 p7 的解释,Ideone 是正确的,因为该条款中的第 4 个要点。
这是来自 N3797 的 § 8.5 p7
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 if T has a non-trivial default constructor, the object is default-initialized;
- if T is an array type, then each element is value-initialized;
- otherwise, the object is zero-initialized.
An object that is value-initialized is deemed to be constructed and thus subject to provisions of this International Standard applying to “constructed” objects, objects “for which the constructor has completed,” etc., even if no constructor is invoked for the object’s initialization.
最佳答案
就 C++14 (N3797) 而言,Ideone 是正确的(参见 Casey 对 C++11 的回答),因为 a[1]
和 a[2]
用A{}
初始化,也就是值初始化,导致i
为0。这来自N3797 § 8.5.1/7:
If there are fewer initializer-clauses in the list than there are members in the aggregate, then each member not explicitly initialized shall be initialized from its brace-or-equal-initializer or, if there is no brace-or-equal initializer, from an empty initializer list (8.5.4). [ Example:
struct S { int a; const char* b; int c; int d = b[a]; };
S ss = { 1, "asdf" };
initializes ss.a with 1, ss.b with "asdf", ss.c with the value of an expression of the form int{} (that is, 0), and ss.d with the value of ss.b[ss.a] (that is, ’s’)
根据 § 8.5.1/1,数组是聚合(聚合是数组...
),因此这适用于数组的初始化。
表达式 T{}
(一个空的初始化列表)根据 § 8.5.4/3 对对象进行值初始化:
Otherwise, if the initializer list has no elements and T is a class type with a default constructor, the object is value-initialized.
我们可以根据 § 8.5/8 确认值初始化使 i
的值为 0:
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 if T has a non-trivial default constructor, the object is default-initialized;
根据 § 8.4.2/4,您的类没有用户提供的默认构造函数:
A function is user-provided if it is user-declared and not explicitly defaulted or deleted on its first declaration.
值得注意的一点是,如果您的默认构造函数是用户提供的并且未初始化 i
,则将使用 8.5/8 的第一个点和 i
将保持未初始化状态,如您在此 example 中所见.
最后,关于比较的小说明。 Ideone 使用几个不同版本的 GCC。使用哪一个会有所不同(如果需要,您可以使用 __VERSION__
检查)。在这种情况下,编译器标志也略有不同。如果 -std=c++1y
存在(我不知道除了使用附加功能之外的检查方法),会有一些 C++14 支持,但不完整支持,所以小的变化(T{}
与 T()
,从大括号或相等初始化器初始化并检查默认初始化的语义约束)可能无法实现。事实上,你甚至可以check the first . Coliru 允许您配置构建命令,所以仅仅说 Coliru 是非常含糊的。
无论哪种方式,在出现足够的 C++14 支持(或至少在它标准化之前)之前,使用 N3797 测试一致性行为都不太值得。在这种情况发生之前,我倾向于坚持使用 N3485。在这个具体示例中,我认为这两个标准的行为之间没有任何区别。请查看 Casey 的回答,了解这两个标准在这个问题上有何不同。你有一个转换构造函数,所以你的对象将在 C++11 中默认初始化。
关于c++ - 该片段在 Coliru 中编译时带有警告,但在 Ideone 中编译正常。哪一个是正确的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20936574/
有人可以告诉我为什么会发生这种情况吗?这里出了什么问题: 我必须打印字符串的一部分,其中要打印的字符串的起始位置和长度在输入中给出。 问题:当我在 devC++ 中运行它时,一切都运行良好并且输出正常
这段代码有问题http://www.spoj.com/problems/BASE/它在 windows 和 linux 上运行良好,但是当我在 ideone 上运行它时,它没有显示任何输出。谁能告诉我
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import
我正在用 C 语言编写一个程序,用于查找两个排序数组的交集。当我在我的机器上使用 GCC 编译并运行该代码时,该代码工作正常,但在 ideone.com 上出现运行时错误。 这是Live link到下
我的程序有什么问题?它在我的 PC 上运行良好,但在 IDEone 中它提供了正确的输出,但显示运行时错误。请帮忙。 #include using namespace std; struct stud
好吧,我在 Ideone 上搞砸了并意外提交了这段代码,但令我惊讶的是它实际上编译并运行输出值 0,here . #include using namespace std; const int fi
我写了下面的代码: #include using namespace std; int main() { int v() return 0; } 我在 ideone 中运行了它,编译
我为我的家庭作业创建了一个程序,我尝试在 ideone.com 上测试它,但我得到一个错误: Exception in thread "main" java.lang.NullPointerExcep
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我最近试图在 ideone.com 中运行这段代码... #include #include using namespace std; int getVal(string name) {
请你告诉我我做错了什么。我认为我的代码工作正常,但未通过 Ideone 测试。 do{ linia = reading.nextLine(); try{ number
考虑下面的程序: #pragma startup foo1 #pragma exit foo2 void foo1() { printf("Called before main\n"); }
import java.util.Scanner; public class Admit { // the main method has minimal dialogue and just
我正在使用 ideone 作为在线 c++ 编译器。当我保存一个代码时,ideone 给出了一个随机名称,例如 lnzr40 稍后可能会造成混淆(当我想打开特定代码时)。我想在编写/保存代码时更改该名
我正在尝试远程指导我办公室的几个人使用 Python,最简单的方法似乎是向他们展示这些简单的 Python 示例如何...... http://wiki.python.org/moin/SimpleP
我正在尝试通过比较 Visual Studio 的输出与 GCC 的输出来调试我在 Visual Studio 中遇到的问题,但我似乎无法获得要在 Ideone 中编译的极其简化的代码版本: #inc
我想建立自己的在线编译器。我想为此使用 Ideone Api。但其 api 以 WSDL 格式提供。我非常努力地尝试,但可以找到任何关于如何从 WSDL 中提取数据的教程。请告诉一些使用 Ideone
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我正在尝试编写一个程序,该程序接受语法不正确的文本(长度低于 990 个字符)作为输入,对其进行更正,然后返回更正后的文本作为输出。我尝试使用在线编译器“ideone”运行该程序,但它返回了很多我不太
以下代码计算输入整数的阶乘。我在 Codeblocks 中运行了 2 个测试用例 - 9 和 2,并得到了正确的输出。对于相同的输入,ideone 将 2 的阶乘输出为 6,但它正确输出 9 的阶乘。
我是一名优秀的程序员,十分优秀!