首先,我想说,根据 cppreference.com,对枚举进行值初始化有点不可能。
根据http://en.cppreference.com/w/cpp/language/value_initialization ,值初始化枚举实际上执行零初始化。然后,根据 http://en.cppreference.com/w/cpp/language/zero_initialization ,对枚举进行零初始化的效果是:
If
Tis a scalar type, the object's initial value is the integral constant zero implicitly converted toT.
但是,整数常数零不能隐式转换为枚举。最终,枚举不能进行值初始化。这听起来很奇怪,并且值初始化枚举确实适用于 VC、GCC 和 clang。那么,标准对此有何规定?
第二,根据http://en.cppreference.com/w/cpp/language/static_cast :
Integer, floating-point, or enumeration type can be converted to any complete enumeration type (the result is unspecified (until C++17) undefined behavior (since C++17) if the value of expression, converted to the enumeration's underlying type, is not one of the target enumeration values)
那么,如果目标枚举没有等于 0 的枚举数,这是否意味着对枚举进行值初始化(如果它确实有效)实际上可能会导致未定义的行为?
最佳答案
评论中给出了答案。下面给出了我试图解释其背后的整个标准的尝试。
To zero-initialize an object or reference of type
Tmeans:
- if
Tis a scalar type (3.9), the object is initialized to the value obtained by converting the integer literal0(zero) toT;
(枚举是标量类型;§3.9/9) 因此,由于转换不是隐式的,我们不是在看第 4 节,而是在第 5.2.9 节;
The result of the expression
static_cast<T>(v)is the result of converting the expressionvto typeT.
§5.2.9/10 然后定义如何将整数值转换为枚举类型。
A value of integral or enumeration type can be explicitly converted to an enumeration type. The value is unchanged if the original value is within the range of the enumeration values (7.2). Otherwise, the resulting value is unspecified (and might not be in that range).
必须证明零在所有枚举的枚举值范围内。
接下来的五个引述取自第 7.2/8 节:
For an enumeration whose underlying type is fixed, the values of the enumeration are the values of the underlying type.
由于所有允许的基础类型在其值范围内都包含零*,因此这会自动给出所需的结果。现在,对于没有固定基础类型的枚举,
Otherwise, for an enumeration where emin is the smallest enumerator and e max is the largest, the values of the enumeration are the values in the range b min to b max , defined as follows:
即我们必须证明 bmin 总是小于或等于零,而 bmax 总是大于或等于零。
Let K be 1 for a two’s complement representation and 0 for a one’s complement or sign-magnitude representation.
b max is the smallest value greater than or equal to max(|e min| − K, |e max|) and equal to 2M − 1, where M is a non-negative integer.
|e max| 是非负数,两个数的最大值至少与两个数一样大。因此 max(|e min| - K, |e max|) 也是非负的,并且 bmax 必须大于或等于该数字 - 所以我们的第一个要求得到满足。
b min is zero if emin is non-negative and −(bmax + K) otherwise.
bmin 显然是零或负数:bmax 是非负数,如上所示,并且 K 是非负的(0 或 1),因此它们和的加法逆是非正的。我们的第二个要求得到满足。最后,
If the enumerator-list is empty, the values of the enumeration are as if the enumeration had a single enumerator with value
0.
这通过设置 emin = emax = 0 得到上述结果。
关于c++ - 值初始化枚举的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27935026/
在我的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初始化方法,但您没有调
我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden
我有用于控制用户任务的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.你能做的最好的事情是:
两个gsub产生不同的结果。谁能解释一下为什么?代码也可在https://gist.github.com/franklsf95/6c0f8938f28706b5644d获得.ver=9999str="\tCFBundleDevelopmentRegion\n\ten\n\tCFBundleVersion\n\t0.1.190\n\tAppID\n\t000000000000000"putsstr.gsub/(CFBundleVersion\n\t.*\.).*()/,"#{$1}#{ver}#{$2}"puts'--------'putsstr.gsub/(CFBundleVersio
我对如何计算通过{%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)但是,当我询问类本身它有哪些实例方法时,我没有看到默认