struct Bar {
template<typename>
void baz() {
}
};
template<typename>
struct Foo {
Bar bar;
Foo() {
bar.baz<int>();
}
};
int main() {
return 0;
}
这段代码编译得很好(在 GCC 4.7 中),但是如果我在调用前加上 bar.baz<int>()与 this-> , baz成为需要用 template 消除歧义的从属名称.
bar.baz<int>(); // OK
this->bar.baz<int>(); // error
this->bar.template baz<int>(); // OK
当然this->bar只能引用Bar bar , 其成员 baz显然是一个模板?为什么要添加this->让这个代码对编译器产生歧义?
附言原来,bar是需要用 this-> 消除歧义的基类模板的数据成员,但为了这个问题,我已经简化了这个例子。
最佳答案
this->bar.baz<int>(); // error
上述声明,在 template<typename T> Foo<T>::Foo() 的定义中, 格式正确,如果启用了 C++11 模式或 C++1y 模式,则应被接受。但根据 C++03,它在技术上是错误的。
两个标准都同意 this是一个依赖于类型的表达式:
C++03 14.6.2.1/1; N3690 14.6.2.1/8:
A type is dependent if it is
a template parameter,
...
a [simple-]template-id in which either the template name is a template parameter or any of the template arguments is a dependent type or an expression that is type-dependent or value-dependent,
[ T是依赖类型,Foo<T> 也是.]
C++03/N3690 14.6.2.2/2:
thisis type-dependent if the class type of the enclosing member function is dependent.
[自 Foo<T>是依赖类型,表达式 this在其成员定义中是依赖于类型的。]
这两个标准都从 14.6.2.2 开始:
Except as described below, an expression is type-dependent if any subexpression is type-dependent.
C++03 只有三种简单的表达方式,描述更准确:
主要表达式(this 和查找名称)
指定自己类型的表达式(如强制转换和新表达式)
具有常量类型的表达式(如文字和 sizeof )。
第一类在C++03 14.6.2.2/3中定义:
An id-expression is type-dependent if it contains:
an identifier that was declared with a dependent type,
a template-id that is dependent,
a conversion-function-id that specifies a dependent type,
a nested-name-specifier that contains a class-name that names a dependent type.
所以孤独的表达bar不依赖:它是一个标识符和一个id-expression,但以上都不适用。
但是this->bar不是id-expression,或者在任何其他C++03 异常中,所以我们必须遵循子表达式规则。由于子表达式 this是类型相关的,包含表达式 this->bar也是依赖于类型的。
但事实上,正如您所注意到的,this->bar 的类型可以在解析模板定义时知道,而无需实例化任何模板参数。它被声明为主模板的成员,因此名称必须绑定(bind)到该成员声明。模板特化可能会使 Foo<T>::bar未声明或以不同的方式声明,但在这种情况下,根本不会使用主模板,而当前定义的 Foo()对于该特化,将被忽略。这就是为什么 C++11 定义了“当前实例化”的概念并将其用于类型相关表达式的传染性的进一步异常(exception)。
N3690 14.6.2.1/1:
A name refers to the current instantiation if it is
in the definition of a class template, a nested class of a class template, a member of a class template, or a member of a nested class of a class template, the injected-class-name of the class template or nested class
in the definition of a primary class template or a member of a primary class template, the name of the class template followed by the template argument list of the primary template (as described below) enclosed in
<>(or an equivalent template alias specialization),...
[第一个项目符号是 Foo是当前的实例化。第二个说Foo<T>是当前的实例化。在本例中,两者命名相同的类型。]
14.6.2.1/4:
A name is a member of the current instantiation if it is
An unqualified name that, when looked up, refers to at least one member of a class that is the current instantiation or a non-dependent base class thereof.
A qualified-id in which ...
An id-expression denoting the member in a class member access expression for which the type of the object expression is the current instantiation, and the id-expression, when looked up, refers to at least one member of a class that is the current instantiation or a non-dependent base class thereof.
[第一个项目符号是 bar单独是当前实例化的成员。第三个子弹说this->bar是当前实例化的成员。]
最后,C++11 为类型相关表达式添加了第四类规则,用于成员访问。 14.6.2.2/5:
A class member access expression is type-dependent if the expression refers to a member of the current instantiation and the type of the referenced member is dependent, or the class member access expression refers to a member of an unknown specialization.
this->bar确实引用了当前实例化的成员,但类型为 Bar被引用的成员不依赖。所以现在this->bar不依赖于类型,名称 baz在 this->bar.baz在模板定义期间作为非依赖名称查找。 template baz 之前不需要关键字.
关于c++ - 为什么只有在符合 "this"条件时,数据成员的模板函数才是从属名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24588207/
类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
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这