草庐IT

Templates

全部标签

c++ - 类 : where to specify it? 函数的默认模板参数

对于C++中的每种情况,我必须在哪里指定类成员函数的默认模板参数(假设声明(当然)在“类主体”中,并且函数定义在类主体之外)2011年:“正常”功能静态函数友元函数在定义中,在声明中还是在两者中? 最佳答案 嗯,根据我创建模板类和方法的经验,您可以这样指定模板函数:templateTMyFunc(T&aArg1,T&aArg2){//...DefinitionGoesHere}typenameT是模板函数的模板参数类型,您需要将该数据类型一致地传递给标记为“T”的每个参数。这意味着aArg2必须是aArg1的任何数据类型。现在,当你

c++ - 友元函数默认模板 : Intel ICPC warning

我有以下测试代码://friendfunction.h#includetemplateclassMyClass{templatefriendinlineconstMyClassmyFunction(constT1&x,constMyClass&y);};template::value>::type>inlineconstMyClassmyFunction(constT0&x,constMyClass&y){std::cout(y);}//friendfunction.cpp#include"friendfunction.h"intmain(intargc,char*argv[]){My

c++ - "empty"可变模板特化的地址

我有一个可变模板成员函数定义为:templateVAlgorithm*CreateAlgorithm(constchar*objectName,constchar*className,Params...par)我想获取Params不包含类型的专用版本的地址(我称之为“空”专用化),即:VAlgorithm*CreateAlgorithm(constchar*objectName,constchar*className)我尝试了几种方法。天真的方式:&AlgorithmFactory::CreateAlgorithm(因为,例如,&AlgorithmFactory::CreateAlgo

c++ - 模板偏特化问题

我正在尝试为数学编程编写一个大小和类型通用vector类。我在部分特化方面遇到问题。当我尝试针对给定大小特化我的vector类的成员方法时出现问题。我可以举一个简单的例子:templateclassTestVector{public:inlineTestVector(){}TestVectorcross(TestVectorconst&other)const;};templateinlineTestVectorTestVector::cross(TestVectorconst&other)const{returnTestVector();}voidtest(){TestVectorve

c++ - 如何在C++中实例化模板

我想从DLL导出模板函数。我知道我可以使用如下模板特化方法。func.hpp/*declare*/templateDLL_EXPORTSTfunc(Tpara);/*specialization*/templateDLL_EXPORTSintfunc(intpara);func.cpptemplateDLL_EXPORTSintfunc(intpara){returnpara;}如果我使用模板特化。我应该重写func每种类型的代码。这不是一个好的解决方案。但这是我从C++Primer中找到的唯一方法.偶尔从别人的代码中找到另一种方法,如下。func.hpp/*declare*/temp

c++ - 通过模板传递函数时推断的返回类型

我的问题是让编译器根据模板传递的函数的返回类型推断函数的返回类型。有什么方法可以调用吗foo(7.3)代替foo(7.3)在这个例子中:#includetemplateVfoo(Tt){returnfunc(t);}intbar(doublej){return(int)(j+1);}intmain(){printf("%d\n",foo(7.3));} 最佳答案 如果你想将bar保留为template参数,恐怕你只能接近它:#includetemplatestructtraits{};templatestructtraits{typ

c++如何为类的模板化转换运算符指定参数

我正在尝试为类的模板化转换运算符指定模板参数,但我似乎无法获得正确的语法。#includeusingnamespacestd;classC{inti_;public:C(inti):i_(i){}templateintget(){returni_+adder;}templateintoperator()(){returni_+adder;}templateoperatorint(){returni_+adder;}//IfIaddadefaultargumenttooperatorint()'saddertemplateparameterthiscompilesfine//(ofcou

c++ - 通用成员函数指针作为另一个类中的模板参数

我的问题类似于this.而'KarrekSB'sanswer实际上对我有所帮助。我有这些类(class):基础.h:classBase{public:Base(){}virtual~Base(){}virtualvoidinit()=0;};A1.h:#include#include"Base.h"usingnamespacestd;classA1:publicBase{public:A1(){}virtual~A1(){};virtualvoidinit(){cout我有另一个类应该能够存储具有任何类型和数量的参数的任何通用成员函数。该类看起来像这样:MFholder.h:#incl

c++ - 解决编译器错误

我有大型计算几何库。它的内核有问题。我们有定义标量taits和自由函数形式的辅助访问器,可以简单地编写cg::epsilon()而不是cg::scalar_traits::epsilon.但问题是在vs2008和vs2010下它有时会争辩说它无法推断出T的模板参数。在cg::epsilon.在LWS中的其他编译器上工作正常。要重现的缩减版本:namespacecg{templateSepsilon();templatedoubleepsilon(){return1;}templatefloatepsilon(){return1;}templatebooleq(Sa,Sb,Seps=cg

c++ - 模板运算符 << 循环

在我的库代码中:classVeryStrictClass{public:templateVeryStrictClass&operator无法重新实现它。这禁止像这样声明函数template/*checktoStringexistance*/VeryStrictClass&operatortoString();}其中toString()不是虚拟的。我有很多类似的类要放入VeryStrictClass。有什么办法可以绕过这个限制吗? 最佳答案 您可以为自己的消息类型使用模板特化。classfoo{};classbar:publicfoo