草庐IT

c++ - 是否应该为类成员访问表达式中的依赖类/命名空间名称延迟名称查找?

coder 2024-02-25 原文

下面的代码被 clang 和 gcc 都拒绝

template<typename T>
void f(T t)
{
    t.Dependent::f(); // clang accepts, gcc rejects
    t.operator Dependent*(); // both reject
}

struct Dependent
{
     void f();
};

struct A : Dependent
{
     operator Dependent*();
};

template void f<A>(A);

我对标准的阅读表明这两种表达方式都应该被接受。

在这两种情况下,Dependent 只能是类型名称。

在这两种情况下,名称Dependent 都将“在对象表达式的类中查找”t。由于 t 是一个依赖于类型的表达式,查找应该推迟到模板被实例化。

有什么我想念的吗?

编辑:如果打算让这样的名称不依赖,那么这个决定的理由是什么?我可以看到,如果实现者不必推迟对 t.operator X::Dependent*t.X::Dependent::f 其中 X 可以是命名空间或类型名称。我不清楚这是当前措辞有意还是无意的副作用。

C++ 工作草案 N3337 的相关引述:

3.4.5 Class member access [basic.lookup.classref]

If the id-expression in a class member access is a qualified-id of the form class-name-or-namespace-name::... the class-name-or-namespace-name following the . or -> operator is first looked up in the class of the object expression and the name, if found, is used. Otherwise it is looked up in the context of the entire postfix-expression. [ Note: See 3.4.3, which describes the lookup of a name before ::, which will only find a type or namespace name. —end note ]

If the id-expression is a conversion-function-id, its conversion-type-id is first looked up in the class of the object expression and the name, if found, is used. Otherwise it is looked up in the context of the entire postfix-expression. In each of these lookups, only names that denote types or templates whose specializations are types are considered.

14.6.2 Dependent names [temp.dep]

Inside a template, some constructs have semantics which may differ from one instantiation to another. Such a construct depends on the template parameters. In particular, types and expressions may depend on the type and/or value of template parameters (as determined by the template arguments) and this determines the context for name lookup for certain names. Expressions may be type-dependent (on the type of a template parameter) or value-dependent (on the value of a non-type template parameter).

[...]

Such names are unbound and are looked up at the point of the template instantiation (14.6.4.1) in both the context of the template definition and the context of the point of instantiation.

14.6.2.1 Dependent types [temp.dep.type]

A name is a member of an unknown specialization if it is

[...]

An id-expression denoting the member in a class member access expression (5.2.5) in which either

— the type of the object expression is the current instantiation, the current instantiation has at least one dependent base class, and name lookup of the id-expression does not find a member of the current instantiation or a non-dependent base class thereof; or

the type of the object expression is dependent and is not the current instantiation.

[...]

A type is dependent if it is

a member of an unknown specialization,

最佳答案

1

我认为您的第一个案例 t.Dependent::f 是这样工作的。首先,我相信(意思是,我不完全确定)14.6.2.1p5 应该说“unqualified-id”而不是“id-expression”。但与此无关,您的名字 Dependent::f 实际上是由两个名字组成的(在标准中,每个嵌套的 nested-name-specifier 后跟一个成员名称为“qualified-id”,即使在语法上,这些都不是合格的 id 产品。因此名称 foo::bar::baz 是一个合格的 id,但也包含 1 个其他“合格的 id”。

DependentDependent::f。前者不是“表示类成员访问表达式中的成员的 id 表达式”,因此您不能简单地将适用于 Dependent::f 的规则应用到 依赖

Dependent 因此是非依赖的,尽管需要在依赖类型中查找它,但必须在定义时找到它。我个人认为我们应该有一个子句说“当查找限定符依赖于类型的限定 ID 时,名称查找会产生一个空结果。”,以优雅地处理这些“强制名称查找立即完成” .所以无论如何,最后,我认为你的第一个案例是错误的,因为没有找到 Dependent (第 3.4 条不能仅仅在第 14 条的头上自行决定名称实际上是依赖的).

2

对于您的另一种情况,operator Dependent,事情就简单多了。您再次拥有两个名称,Dependentoperator Dependent。同样,我没有发现任何说明 Dependent 是这里的依赖名称(我不确定这是否是错误的。这超出了我的范围)。

运算符函数名称的名称查找比较(例如,名称查找哈希表的相等函数)是“它们是由相同类型构成的转换函数 ID”(3.8)。这意味着为了形成名称本身(还没有进行名称查找!),您不仅必须像标识符那样给出词汇拼写,而且还必须提供类型标识,这需要由依赖

t.operator Dependent* 中依赖 id-expression 的查找被延迟仅仅意味着语义类型比较被延迟。试试这个,应该可以正常工作

struct Dependent; // forward decl at global scope
t.operator Dependent*(); // in function template

你的跟进

If it is intended that such a name is not dependent, what is the rationale for this decision? I can see that it makes life easier for the implementor if they do not have to defer evaluation of a construct like t.operator X::Dependent* or t.X::Dependent::f where X could be either a namespace or a type name.

我不知道其中的原理,但我认为您已经给出了一个很好的观点。这看起来非常符合在查找非限定名称时跳过依赖基类的规则。我认为适用于该案例的理由也适用于该案例。对于 程序员 来说,它更容易在函数模板上进行推理,尤其是。

struct Dependent;

template<typename T>
void f(T t)
{
    t.Dependent::f();
    t.operator Dependent*();
}

代码看起来不错,但是如果 T 碰巧有一个 Dependent 成员,突然 Dependent 会有一个不同的绑定(bind)(因为首先我们被告知查看 t 的类,然后查看周围的范围)。根据我目前对模板规则的理解,上面总是引用周围范围的 Dependent,所以上面的代码对于那个陷阱是“安全的”。

关于c++ - 是否应该为类成员访问表达式中的依赖类/命名空间名称延迟名称查找?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18218475/

有关c++ - 是否应该为类成员访问表达式中的依赖类/命名空间名称延迟名称查找?的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby - 其他文件中的 Rake 任务 - 2

    我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时

  3. ruby-on-rails - Ruby net/ldap 模块中的内存泄漏 - 2

    作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代

  4. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  5. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  6. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  7. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  8. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  9. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  10. ruby-on-rails - Rails 应用程序中的 Rails : How are you using application_controller. rb 是新手吗? - 2

    刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr

随机推荐