草庐IT

c++ - "template"不需要关键字? [gcc/clang/Comeau 错误?]

coder 2023-11-14 原文

这是测试代码

template <class T> void f()
{
  T t;
  t.f<T>(0); //compiles even without the "template" keyword, what am I missing?
}

class abc
{
  public:
  template <typename T>
  void f (int){}
};

int main()
{
  f<abc>();
}

我正在使用 g++ 4.4.6。谢谢

P.S:我已经大大编辑了我的问题。请不要介意。

编辑:我向 EDG 的人问了这个问题,这是 Mike Herrick 不得不说的

We do diagnose this as an error in --strict mode as well as any mode that enables dependent name lookup (e.g., --dep_name, --parse_templates). Dependent name lookup is disabled in GNU emulation modes, so we don't emit this error in that case.

Dependent name processing requires that nonclass prototype instantiations be enabled (see below). As with nonclass prototype instantiations, enabling dependent name lookup is likely to cause compilation errors when compiling code that was not written with the feature in mind.

The dependent name lookup rules require that nondependent names be looked up at the point of use in the template definition, and that overload resolution be performed on nondependent calls at that point. For dependent calls, the set of names considered is the set visible at the point of use in the template definition plus any names made visible by argument-dependent lookup at the point of instantiation. Note that built-in types have no associated namespaces, so calls with only built-in types can only resolve to names visible in the template definition. Furthermore, names from dependent base classes are not visible to unqualified lookups.

下面说明了一些最常见的代码问题 使用从属名称查找时:

template <class T> struct B {
    void f();
  };

template <class T> struct A : public B<T> {
    X x;  // error: X not visible yet (formerly an error in strict mode)
    void g() {
      f();        // error: B<T>::f not visible
      this->f();  // must be written this way
      h(1);  // error: h(int) not visible using argument-dependent lookup
    }
  };
struct X {};
void h(int);
A<int> ai;

最佳答案

template关键字 都是必需的,因为 t是从属名称,因为 f<T>是依赖成员函数模板特化。相关规范分散在第 14 条中,但从 §14.2/4 开始(在 C++03 和 C++11 中)。

问题是由于不正确的名称查找:gcc 正在查找 namespace 作用域函数模板 f在声明点,然后在实例化点解析 f abc的成员函数模板.

如果您重命名命名空间作用域函数模板或成员函数模板,您将从编译器获得正确的行为。

这是一个长期存在的 gcc 错误:

Code with missing "template" keyword wrongly accepted

Weird clash with same names in different scopes

另请参阅针对这两个问题解决的许多重复错误。我没有看到为此打开的 Clang 错误。

函数模板中的名称查找在 C++03 中未指定;有许多关于该主题的缺陷报告,规范在 C++11 中进行了重大更改,以澄清细节和极端情况并修复细微问题。

关于c++ - "template"不需要关键字? [gcc/clang/Comeau 错误?],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8986498/

有关c++ - "template"不需要关键字? [gcc/clang/Comeau 错误?]的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是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

  2. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  3. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从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""-

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

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

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

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

  6. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  7. ruby - rspec 需要 .rspec 文件中的 spec_helper - 2

    我注意到像bundler这样的项目在每个specfile中执行requirespec_helper我还注意到rspec使用选项--require,它允许您在引导rspec时要求一个文件。您还可以将其添加到.rspec文件中,因此只要您运行不带参数的rspec就会添加它。使用上述方法有什么缺点可以解释为什么像bundler这样的项目选择在每个规范文件中都需要spec_helper吗? 最佳答案 我不在Bundler上工作,所以我不能直接谈论他们的做法。并非所有项目都checkin.rspec文件。原因是这个文件,通常按照当前的惯例,只

  8. ruby - 如何在 Lion 上安装 Xcode 4.6,需要用 RVM 升级 ruby - 2

    我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121

  9. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  10. ruby-on-rails - 相关表上的范围为 "WHERE ... LIKE" - 2

    我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que

随机推荐