jjzjj

explicit-instantiation

全部标签

c++ - 我可以使用 decltype() 来避免显式模板实例化中的代码重复吗?

我有一个很长的模板函数声明:templatevoidfoo(lotsofargs,goinhere,andevenmore,ofthesearguments,theyjust,dontstop);没有重载。我想显式实例化它。我可以写(比如T=int):templatevoidfoo(lotsofargs,goinhere,andevenmore,ofthesearguments,theyjust,dontstop);但我真的不想复制那么长的声明。我希望喜欢能够说出类似的话:templateusingbar=decltype(foo);然后:templatebar;现在,第一行编译(GC

c++ - 在模板中初始化静态成员

这是一个最小的例子:#includestructB{B(){x=42;}staticintx;};intB::x;templatestructA{intfoo(){returnb.x;}staticBb;};templateBA::b;//templatestructA;//explicitinstantiationwithN=2(!)intmain(intargc,char**argv){std::cout().foo()此程序使用g++4.9.2写入42,但使用VisualStudio2015RC写入0。另外,如果我取消显式实例化的注释,VS2015RC也会给出42,这很有趣,因为

C++:我可以做一个赋值运算符 "explicit"吗

我的任务是迁移C++类库中的错误处理概念。以前简单返回bool(成功/失败)的方法应修改为返回一个Result对象,该对象传达机器可读的错误代码和人类可读的解释(以及更多在这里无关紧要的内容)。遍历数千行代码很容易出错,因此我尝试从编译器获得对此任务的最佳支持。我的结果类在其他成员方法中有一个从代码构造结果的构造函数和代码的赋值运算符:classResult{public:typedefunsignedlongResultCode;explicitResult(ResultCodecode);//(1)Result&operator=(ResultCodecode);//(2)};备注

c++ - 在使用 Clang 编译 CRTP Singleton 时,如何解决声称缺少 "explicit instantiation declaration"的问题?

我们正在使用curiouslyrecurringtemplatepattern实现单例。但是,在最近的Clang版本中,我们收到了-Wundefined-var-template警告。建议的修复方法是添加“显式实例化声明”。我试图这样做,但后来在定义单例模板类成员变量的编译单元中出现有关“实例化后的显式特化”的错误。解决此警告突出显示的问题的适当构造是什么?简化详细信息(已删除大部分逻辑,以制作MCVE):单例基础.hh:templateclassSingletonBase{public:staticT*get_instance(){if(!instance_){instance_=T

c++ - 一大堆显式的函数模板实例化怎么才能简明扼要呢?

我正在编写一个C++库,其中包含许多我想显式实例化和导出多个类型参数的函数模板。在我的特殊情况下,我有很多数字函数模板,我想为float单独实例化和编译它们。,double,和longdouble.它们看起来像这样:templateTcalculate_a(Tx){...}templateTcalculate_b(Tx,Ty){...}//...如果我有M个函数模板和N个底层类型,那么我有M*N个显式实例要输入。是否可以更简洁地编写这些实例化?我目前的解决方案是使用预处理器宏来执行给定类型的所有实例化:#defineEXPLICITLY_INSTANTIATE(T)\templateT

c++ - 外部模板 'inconsistent explicit instantiations'

给定#include//CaseI:errorerrorC2961:'std::vector>':inconsistentexplicitinstantiations,apreviousexplicitinstantiationdidnotspecify'externtemplate'templateclassstd::vector;externtemplateclassstd::vector;//CaseII:fine//externtemplateclassstd::vector;//templateclassstd::vector;//CaseIII:fine//externte

c++ - 主区域 : "master region may not be closely nested inside of work-sharing or explicit task region" 的 OpenMP for 循环

我有以下代码,我认为它应该显示一个进度条来近似整个过程的进度(因为循环的每个并行线程应该以大致相同的速度进行)#pragmaompparallelforfor(longintx=0;x但是,我收到以下错误:warning:masterregionmaynotbecloselynestedinsideofwork-sharingorexplicittaskregion[enabledbydefault]现在,当我运行代码时,我确实得到了想要的结果。但我不喜欢警告。为什么这会给我一个警告,是否有更好的方法来完成此操作?谢谢! 最佳答案

c++ - 'instantiation' 对模板的意义

Notethatcodeisinstantiatedonlyformemberfunctionsthatarecalled.Forclasstemplates,memberfunctionsareinstantiatedonlywhentheyareused.上面的引文来自这本书:AddisonWesley的C++Templates。我想了解行话“代码被实例化”的含义。这是否意味着只保留特定的内存,或者只编译该代码或其他什么? 最佳答案 这是一个非常有趣的问题,应该在编译器如何处理模板的更广泛的上下文中完成。基本上模板是编译器从中生

c++ - CLion "Instantiating an unknown structure without reference"但编译正常

我一直在尝试使用CLion编辑器和MinGW在Windows10(64位)上使用HDF5设置我的cmake项目。经过大量时间尝试正确设置我的CMakeLists文件后,我得到了一些工作-代码编译,mingw32-make或cmake没有错误。但是,我仍然在CLion中遇到带有红色下划线的错误,这些错误似乎对构建没有任何影响,但我感觉它们存在是因为我做错了什么。(除了使用C++进行类项目外,我还很陌生)这是我的CMakeLists.txtcmake_minimum_required(VERSION2.8)project(testProject)add_definitions(-std=c

C++ : Calling a child method from parent instantiation

在我的代码中,我实现了这些类:classA{public:virtualintfun(){return0;}}classB:publicA{public:virtualintfun(){return1;}}还有这些函数:voidoperation(Aa){printf("%d\n",a.fun());}intmain(){Bb;operation(b);return0;}可以看到,B类继承了A类,并实现了虚继承方法fun()。主类调用一个以A为参数的函数,并调用fun()方法,参数为B对象。在执行时,我希望打印字符串"1",但它是"0"(即使它是传递给的B对象操作()).我需要这样做,