- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
考虑以下代码:
struct A {
int x;
};
int main() {
A a;
A b{a};
}
这个程序在 C++11 标准下是否良构?在我的 N3797 拷贝中它说
8.5.4 List initialization
[dcl.init.list]
3: List-initialization of an object or reference of type
T
is defined as follows:
- IfT
is an aggregate, aggregate initialization is performed (8.5.1).
- Otherwise, ifT
is a specialization ofstd::initializer_list<E>
, ...
- Otherwise, ifT
is a class type, constructors are considered. The applicable constructors are enumerated and the best one is chosen using overload resolution. If a narrowing conversion is required to convert any of the types, the program is ill-formed.
- Otherwise, if the initializer list has a single element of typeE
and eitherT
is not a reference type or it is reference-related toE
, the object or reference is initialized from that element; if a narrowing conversion is required to convert the element toT
, the program is ill-formed.
- Otherwise, ifT
is a reference type, a pr-value temporary of the type reference byT
is copy-list-initialized or direct-list-initialized, depending on the kind of initialization for the reference, and the reference is bound to that temporary.
- Otherwise, if the initializer list has no elements, the object is value-initialized.
- Otherwise, the program is ill-formed.
这个例子的要点是,类型是一个聚合,但是列表初始化应该调用复制构造函数。在 gcc 4.8
和 gcc 4.9
,在 C++11 标准下,它失败了:
main.cpp: In function ‘int main()’:
main.cpp:7:8: error: cannot convert ‘A’ to ‘int’ in initialization
A b{a};
^
并说A is not convertible to int
或类似的,因为聚合初始化失败。在 gcc 5.4
,它在 C++11 标准下运行良好。
关于 clang
你会得到类似的错误 clang-3.5
, 3.6
, 它开始工作于 clang-3.7
.
我知道它在 C++14 标准下是良构的,并且在缺陷报告中提到了它 here .
但是,我不明白为什么这被认为是标准中的缺陷。
标准写的时候,
“如果 X
,则执行 foo 初始化。否则,如果 Y
,则执行 bar 初始化,....否则,程序格式错误。”,
这是否意味着如果X
成立,但无法执行 foo 初始化,那么我们应该检查是否 Y
持有,然后尝试栏初始化?
这将使示例工作,因为当聚合初始化失败时,我们不匹配 std::initializer_list
,我们匹配的下一个条件是“T
是类类型”,然后我们考虑构造函数。
请注意,这确实似乎是它在这个修改后的示例中的工作方式
struct A {
int x;
};
int main() {
A a;
const A & ref;
A b{ref};
}
在 C++11 和 C++14 标准中,所有相同的编译器都以与前面示例相同的方式处理它。但似乎 CWG 缺陷记录中修改后的措辞不适用于此案例。内容如下:
If
T
is a class type and the initializer list has a single element of typecv T
or a class type derived fromT
, the object is initialized from that element.
http://open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#1467
但在第二个代码示例中,初始化列表在技术上包含 const T &
.所以我不知道它是如何工作的,除非在聚合初始化失败后,我们应该尝试构造函数。
我错了吗?聚合初始化失败后是否不应该尝试构造函数?
这是一个相关的例子:
#include <iostream>
struct B {
int x;
operator int() const { return 2; }
};
int main() {
B b{1};
B c{b};
std::cout << c.x << std::endl;
}
在clang-3.6
, gcc-4.8
, gcc-4.9
, 它打印 2
,并在 clang-3.7
, gcc-5.0
它打印 1
.
假设我错了,并且在 C++11 标准中,聚合的列表初始化应该是聚合初始化而不是别的,直到引入缺陷报告中的新措辞,这是否是一个错误发生即使我选择 -std=c++11
在较新的编译器上?
最佳答案
When the standard writes,
"If X, foo-initialization is performed. Otherwise, if Y, bar-initialization is performed, .... Otherwise, the program is ill-formed.",
doesn't this mean that if X holds, but foo-initialization cannot be performed, then we should check if Y holds, and then attempt bar-initialization?
不,它没有。把它想象成实际的代码:
T *p = ...;
if(p)
{
p->Something();
}
else
{ ... }
p
不是 NULL。这也不意味着它是一个有效的指针。如果 p
指向一个被销毁的对象,p->Something()
失败不会导致您跳到 else
。您有机会在这种情况下保护通话。
所以你会得到未定义的行为。
这里也是一样。如果 X,则执行 A。这并不意味着如果 A 失败会发生什么;它告诉你去做。如果做不到……你就完蛋了。
关于c++ - 列出聚合的初始化 : when can it invoke copy constructor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39005700/
我有以下代码: Public Delegate Sub SetStatusBarTextDelegate(ByVal StatusText As String) Private Sub SetStat
在调用 Invoke-RestMethod 时使用 Powershell,例如: Invoke-RestMethod -Method Get -Uri "https://google.com/api/
我正在尝试将 Winform 应用程序转换为控制台应用程序。 Winform 应用程序有一个委托(delegate)处理程序。如何在 console 应用程序中编写相同的功能? this.Invoke
在 WPF 中,Dispatcher.Invoke 和直接在控件实例上调用的 Invoke 有什么区别。据我了解,调度程序负责处理线程的消息,Control.Invoke 是否会继续调用 Dispat
我正在研究性能监控系统,它可以将其例程注入(inject)现有程序集。为此,我试图了解 dalvik 代码的工作原理。 下面是我要完成的工作的示例。输入类可能如下所示: class MyClass{
我正在使用 powershell 命令来执行脚本和 cmdlet。因此,在执行 cmdlet 时,我使用了 powershell.invoke,而在执行脚本时,我使用了 pipeline.invoke
有人能解释一下 Invoke-Expression $test 之间的区别吗?和 Invoke-Expression -Command $test ? 变量测试是: $test = "notepad
我有四个类,即 MapperOne、ReducerOne、MapperTwo、ReducerTwo。我想要其中的一个链。 MapperOne-->ReducerOne-->输出文件生成,输入到Mapp
我正在阅读 Java ForkJoin 框架。不直接在 ForkJoinTask 的实现上调用 invoke()(例如 RecursiveTask),而是实例化 ForkJoinPool 有什么额外的
我在调用 Invoke-SqlCmd 时遇到问题,因为它包含第二个 Invoke-SqlCmd 调用: function Get-Foo { $query=` @" WITH data AS (
有人知道如何解决这个问题吗?我创建了一个客户端来使用网络服务。客户端代码为: package cliente; import java.util.List; import handler.Header
我希望使用 P/Invoke 来允许我的 C# 程序集与 native C 库互操作;这需要是跨平台的(即 Mono),因此不能使用混合模式程序集。我想知道使用不安全的 P/invoke 调用并在不安
一般来说,我对使用 Invoke-RestMethod/Invoke-WebRequest 比较陌生 - 我认为这是以下问题的重要背景。 我正在调用如下电话: $Headers = @{ "A
在 Jenkins 的一个自由风格项目(不是说 Maven2/3 项目)中,我有两个可能的构建步骤: 调用 Maven 3 调用顶级 Maven 目标 在不同的安装中,我有不同的组合(有些两者都有,有
这是完整的错误: e: C:\Users\HP\AndroidStudioProjects\MoneyManager\app\src\main\java\com\cruxrepublic\moneym
我正在编写 jQuery 插件并将它们与 AJAX 集成。我正在减少脂肪并专注于基础知识: (function($) { function MyPlugin(el, options) {
有人可以建议我如何处理这条消息吗? CA1060 Move P/Invokes to NativeMethods class Because it is a P/Invoke method, 'UCo
在java中我们可以“用类名调用一个静态方法”也可以“用一个对象调用一个静态方法”java中“用类名调用静态方法”和“用对象调用静态方法”有什么区别? 最佳答案 没有区别,但建议以静态方式调用 sta
尝试从对话框中的 EditText 获取 Edit Text 的值,但一次又一次地出现此错误 Attempt to invoke virtual method 'android.text.Editab
我正在开发一款扑翼应用程序。在出现此错误之前,读取和写入FireStore数据库没有任何问题,但随后突然出现错误(如下所示),并阻止我读取或写入数据库。我一直在寻找答案,但不幸的是,我找不到任何可以解
我是一名优秀的程序员,十分优秀!