[编辑以显示 .cpp 和 hpp 之间的拆分]
// file.hpp
class Base {
public:
virtual ~Base(void);
Base(void);
Base(const Base&) = default;
};
template<typename T>
class Derived: public Base {
public:
Derived(void);
bool func(void);
};
// file.cpp
#include "file.hpp"
Base::~Base(void) {}
Base::Base(void) {}
template<typename T>
bool Derived<T>::func(void) {return true;}
template<typename T>
Derived<T>::Derived(void) {}
// required to avoid linker errors when using `Derived` elsewhere
template class Derived<int>;
最后一行在 Clang v8.0 中导致以下编译器警告 warning: explicit template instantiation 'Derived<int>' will emit a vtable in every translation unit [-Wweak-template-vtables]
我的理解是因为Base至少有一个外联虚方法,这里所有类的 vtables 都将锚定到这个翻译单元,因此 this guidance在 LLVM 编码标准中。 那么为什么会生成此警告?
在 Godbolt 上查看我正在使用的特定编译器版本:https://godbolt.org/z/Kus4bq
我在 SO 上发现的每个类似问题都是针对没有外联虚拟方法的类,因此我无法找到答案。
最佳答案
编辑:我认为这不是 Clang 中的错误,而是 Itanium C++ ABI 要求的结果:https://itanium-cxx-abi.github.io/cxx-abi/abi.html#vague-itemplate此部分在 computeKeyFunction 中的 RecordLayoutBuilder.cpp 中的 Clang 源代码中引用:
Template instantiations don't have key functions per Itanium C++ ABI 5.2.6. Same behaviour as GCC.
Itanium 规范指出类模板实例化将存储在目标文件的 COMDAT 部分中。 COMDAT 部分用于存储同一对象的多个定义,然后可以在链接时统一这些定义。如果模板按照我在回答中预期的方式编译,并通过一个关键函数将其锚定到特定的翻译单元,那么这将不符合此 ABI。
我确实认为该警告没有帮助,但由于它不是 -Wall 或 -Wextra 的一部分,所以我不太介意。
(以下为原帖)
我倾向于认为这是由于 Clang 中的错误所致,此处报告:https://bugs.llvm.org/show_bug.cgi?id=18733
如果链接断开,请在此处重新发布内容:
Rafael Ávila de Espíndola 2014-02-05 00:00:19 PST
Given
template<typename T>
class foo {
virtual ~foo() {}
};
extern template class foo<int>;
template class foo<int>;
clang warns:
test.cpp:6:23: warning: explicit template instantiation 'foo<int>' will emit a vtable in every translation unit [-Wweak-template-vtables]
extern template class foo<int>;
^
1 warning generated.
note that the warning points to the explicit template instantiation declaration, but is triggered by the definition. This should probably be checking if the definition is in a header. In a .cpp file there is only one translation unit that sees it.
Comment 1 David Faure 2016-02-13 12:21:27 PST
Yes, this false positive is indeed annoying. Thanks for reporting it. Clang developers: thanks for fixing it :-)
对于其他人的意见,我将不胜感激,但我同意错误报告者的观点,即在这种情况下,此警告似乎是虚假的。
尽管错误报告的最后评论称它已修复,但该错误仍以"new"状态列出,所以我不认为它已修复。
关于c++ - 为什么显式模板实例化会在存在外线虚拟时导致 weak-template-vtables 警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56041900/
类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
我有一个模型: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返
我试图使用yard记录一些Ruby代码,尽管我所做的正是所描述的here或here#@param[Integer]thenumberoftrials(>=0)#@param[Float]successprobabilityineachtrialdefinitialize(n,p)#initialize...end虽然我仍然得到这个奇怪的错误@paramtaghasunknownparametername:the@paramtaghasunknownparametername:success然后生成的html看起来很奇怪。我称yard为:$yarddoc-mmarkdown我做错了什么?
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA
它不等于主线程的binding,这个toplevel作用域是什么?此作用域与主线程中的binding有何不同?>ruby-e'putsTOPLEVEL_BINDING===binding'false 最佳答案 事实是,TOPLEVEL_BINDING始终引用Binding的预定义全局实例,而Kernel#binding创建的新实例>Binding每次封装当前执行上下文。在顶层,它们都包含相同的绑定(bind),但它们不是同一个对象,您无法使用==或===测试它们的绑定(bind)相等性。putsTOPLEVEL_BINDINGput
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串