C++14 草案 (N3936) 在 §3.2/3 中声明:
A variable x whose name appears as a potentially-evaluated expression ex is odr-used unless applying the lvalue-to-rvalue conversion (4.1) to x yields a constant expression (5.19) that does not invoke any non-trivial functions and, if x is an object, ex is an element of the set of potential results of an expression e, where either the lvalue-to-rvalue conversion (4.1) is applied to e, or e is a discarded-value expression (Clause 5).
这对我来说没有任何意义:如果表达式 e 是 discarded-value 表达式 取决于上下文,其中 e。 expression-statement(第 6.2 节)中使用的每个表达式都是 discarded-value 表达式。 左值到右值的转换是否应用于e也取决于e所使用的上下文。
此外,一个表达式出现在另一个表达式的潜在结果集合中意味着什么。一个人需要一个表达式相等的概念才能确定一个集合的成员。但我们没有参照透明性,所以我看不出这是如何实现的。
为什么从 C++11 改为 C++14?这应该如何解释?就目前而言,这没有任何意义。
最佳答案
非正式地,odr-use 变量的含义如下:
If any expression anywhere in the program takes the address of or binds a reference directly to an object, this object must be defined.
在最新版本的规范 §3.2 中已经阐明(见 Draft C++14 on GitHub):
2 An expression is potentially evaluated unless it is an unevaluated operand (Clause 5) or a subexpression thereof. The set of potential results of an expression
eis defined as follows:
- If
eis an id-expression (5.1.1), the set contains onlye.- If
eis a class member access expression (5.2.5), the set contains the potential results of the object expression.- If
eis a pointer-to-member expression (5.5) whose second operand is a constant expression, the set contains the potential results of the object expression.- If
ehas the form (e1), the set contains the potential results of e1.- If
eis a glvalue conditional expression (5.16), the set is the union of the sets of potential results of the second and third operands.- If
eis a comma expression (5.18), the set contains the potential results of the right operand.- Otherwise, the set is empty.
[ Note: This set is a (possibly-empty) set of id-expressions, each of which is either
eor a subexpression ofe.[ Example: In the following example, the set of potential results of the initializer of
ncontains the firstS::xsubexpression, but not the secondS::xsubexpression.struct S { static const int x = 0; }; const int &f(const int &r); int n = b ? (1, S::x) // S::x is not odr-used here : f(S::x); // S::x is odr-used here, so // a definition is required—end example ] —end note ]
3 A variable
xwhose name appears as a potentially-evaluated expressionexis odr-used byexunless applying the lvalue-to-rvalue conversion (4.1) toxyields a constant expression (5.19) that does not invoke any nontrivial functions and, ifxis an object,exis an element of the set of potential results of an expressione, where either the lvalue-to-rvalue conversion (4.1) is applied toe, oreis a discarded-value expression (Clause 5).
C++11 中的§3.2/2 内容如下:
An expression is potentially evaluated unless it is an unevaluated operand (Clause 5) or a subexpression thereof. A variable whose name appears as a potentially-evaluated expression is odr-used unless it is an object that satisfies the requirements for appearing in a constant expression (5.19) and the lvalue-to-rvalue conversion (4.1) is immediately applied.
这些措辞的问题是DR 712 .考虑这个例子:
struct S {
static const int a = 1;
static const int b = 2;
};
int f(bool x) {
return x ? S::a : S::b;
}
由于 S::a 和 S::b 是左值,条件表达式 x ? S::a : S::b 也是一个左值。这意味着左值到右值的转换不会立即应用于 S::a 和 S::b,而是应用于结果的条件表达式。这意味着按照 C++11 的措辞,这些静态数据成员是 odr-used 并且需要定义。但实际上只使用了值,因此不需要定义静态数据成员 - 声明就足够了。 C++14 草案的新措辞解决了这个问题。
没有。在以下示例中,变量 S::a 仍然是 odr-used:
struct S { static constexpr int a[2] = {0, 1}; };
void f() {
auto x = S::a[0];
}
因此我提交了一个新的 issue将以下项目符号添加到 §3.2/2:
- if
eis a glvalue subscripting expression (5.2.1) of the formE1[E2], the set contains the potential results ofE1.
关于c++ - 何时在 C++14 中使用变量 odr?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23491781/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po