考虑以下代码:
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
Tis defined as follows:
- IfTis an aggregate, aggregate initialization is performed (8.5.1).
- Otherwise, ifTis a specialization ofstd::initializer_list<E>, ...
- Otherwise, ifTis 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 typeEand eitherTis 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, ifTis a reference type, a pr-value temporary of the type reference byTis 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
Tis a class type and the initializer list has a single element of typecv Tor 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/
在我的gem中,我需要yaml并且在我的本地计算机上运行良好。但是在将我的gem推送到rubygems.org之后,当我尝试使用我的gem时,我收到一条错误消息=>"uninitializedconstantPsych::Syck(NameError)"谁能帮我解决这个问题?附言RubyVersion=>ruby1.9.2,GemVersion=>1.6.2,Bundlerversion=>1.0.15 最佳答案 经过几个小时的研究,我发现=>“YAML使用未维护的Syck库,而Psych使用现代的LibYAML”因此,为了解决
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调
我有用于控制用户任务的Rails5API项目,我有以下错误,但并非总是针对相同的Controller和路由。ActionController::RoutingError:uninitializedconstantApi::V1::ApiController我向您描述了一些我的项目,以更详细地解释错误。应用结构路线scopemodule:'api'donamespace:v1do#=>Loginroutesscopemodule:'login'domatch'login',to:'sessions#login',as:'login',via::postend#=>Teamroutessc
我正在阅读一本关于Ruby的书,作者在编写类初始化定义时使用的形式与他在本书前几节中使用的形式略有不同。它看起来像这样:classTicketattr_accessor:venue,:datedefinitialize(venue,date)self.venue=venueself.date=dateendend在本书的前几节中,它的定义如下:classTicketattr_accessor:venue,:datedefinitialize(venue,date)@venue=venue@date=dateendend在第一个示例中使用setter方法与在第二个示例中使用实例变量之间是
如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:
我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我
我正在写一篇关于在Ruby中几乎一切都是对象的博客文章,我试图通过以下示例来展示这一点:classCoolBeansattr_accessor:beansdefinitialize@bean=[]enddefcount_beans@beans.countendend所以从类中我们可以看出它有4个方法(当然,除非我错了):它可以在创建新实例时初始化一个默认的空bean数组它可以计算它有多少个bean它可以读取它有多少个bean(通过attr_accessor)它可以向空数组写入(或添加)更多bean(也通过attr_accessor)但是,当我询问类本身它有哪些实例方法时,我没有看到默认
我去了这个website查看Rails5.0.0和Rails5.1.1之间的区别为什么5.1.1不再包含:config/initializers/session_store.rb?谢谢 最佳答案 这是删除它的提交:Setupdefaultsessionstoreinternally,nolongerthroughanapplicationinitializer总而言之,新应用没有该初始化器,session存储默认设置为cookie存储。即与在该初始值设定项的生成版本中指定的值相同。 关于
我有一个数组数组,想将元素附加到子数组。+=做我想做的,但我想了解为什么push不做。我期望的行为(并与+=一起工作):b=Array.new(3,[])b[0]+=["apple"]b[1]+=["orange"]b[2]+=["frog"]b=>[["苹果"],["橙子"],["Frog"]]通过推送,我将推送的元素附加到每个子数组(为什么?):a=Array.new(3,[])a[0].push("apple")a[1].push("orange")a[2].push("frog")a=>[[“苹果”、“橙子”、“Frog”]、[“苹果”、“橙子”、“Frog”]、[“苹果”、“