草庐IT

c++ - 如何区分具有非类型参数的模板重载?

这里有两个模板函数,它们的区别仅在于它们的模板参数。其余参数完全相同。templatevoidtemplate_const(int&a,int&b){a=a&module;b=b%module;}templatevoidtemplate_const(int&a,int&b){intw;if(x){w=123;}elsew=512;a=a&w;b=b%w;}当我尝试这样称呼他们时template_const(a,b)或template_const(a,b)编译器告诉我调用不明确。如何调用这两个函数? 最佳答案 正如@jogojapan

c++ - 类和函数模板实例化的 Visual Studio dll 导出问题

我在win7中使用VS2008,在CentOS18中使用g++4.7。只有在Windows上使用动态共享库时才会出现此问题。当我将它转换为静态库时,程序链接正常。我知道在共享库中,模板函数/类应该在头文件中定义,或者模板类型(参数)的模板实例化应该通过编译单元提供。我选择了后者。我以前做过,我经历过Whycantemplatesonlybeimplementedintheheaderfile?C++SharedLibrarywithTemplates:Undefinedsymbolserror但我无法弄清楚为什么在我将库转换为dll后在Windows中它无法解析符号:错误LNK2019

c++ - 使用可变参数宏或模板来实现一组功能

我有一组用于实例化和初始化一组对象的方法。除了传递给Init函数的参数数量外,它们看起来几乎相同:ObjectType*CreateObjectType(Arg1a1,Arg2arg2,...ArgNaN){ObjectType*object=newObjectType();[...]object->Init(this,a1,a2,...,aN);[...]returnobject;}请注意,除了传递给Init函数外,不能在任何地方使用参数。我想找到一种方法来实现所有这些,而不必为每种对象类型重复代码。我尝试使用可变参数宏,结果如下(无效):#defineCREATE_OBJECT_I

c++ - 模板特化站点报告 "too few template-parameter-lists"错误

代码某处有错误,但我不知道如何解决。它说“模板参数列表太少”。我不明白哪个是错误的。代码如下:#if!defined(VECTOR_H_INCLUDED)#defineVECTOR_H_INCLUDED#include//forsize_tnamespaceVec{classVector_base{public:explicitVector_base(){}};templateclassVector:publicVector_base{typedefVectorME;explicitVector(T,T,T);doubledot(constME&v)const;T&operator[]

c++ 模板类继承问题

我在编译一段可以简单化如下的代码时遇到错误:#includetemplateclassA{protected:TprotectedValue;templateclassinsideClass{public:TTinsideClassValue;};};templateclassB:publicA{public:voidprint(Tt){insideClassic;//b;b.print(v);return0;};编译器(g++)给出以下错误:main.C:Inmemberfunction‘voidB::printA()’:main.C:23:4:error:‘insideClass’

c++ - 使用 typedeffing 模板化基类来简化代码是一种好习惯吗?

最近在处理许多模板化类并从它们派生时,我发现自己“发明”了这个简单的结构。我不确定这是常见做法,还是我在脖子上系了一根绳子。templateclassBase{};templateclassDerived:publicBase{typedefBaseBase;};我发现如果Base它特别有用类有自己的typedefs对于某些类型。例如:templateclassBase{typedefTScalar;typedefMatrixMatrix;};然后很容易将类型“导入”到Derived中.它节省了重新键入模板签名。例如:templateclassDerived:publicBase{ty

c++ - 将模板化 T 定义为指针

我想使用这段代码定义一些通用指针(?但不是空指针):classA{templateusingptr=T*;usingubyte=uint8_t;public:constptrgetColor1()const{return&colors[0];}constubyte*getColor2()const{return&colors[0];}private:ubytecolors[4];};但是,getColor1()不会编译。这两个函数有什么区别?海湾合作委员会说:error:invalidconversionfrom'constubyte*{akaconstunsignedchar*}'t

c++ - int 参数为 : conditional expressions ignored? 的意外模板行为

以下代码未按预期工作(或至少如我所料)。我尝试的所有g++版本都在模板递归限制下失败。输出似乎表明条件语句被忽略,并且无论P的值如何都使用最后的elseblock。templateinlineREALconst_pow(REALvalue);templateinlineREALconst_pow(REALvalue){return1.0;}templateinlineREALconst_pow(REALvalue){returnvalue;}templateinlineREALconst_pow(REALvalue){returnvalue*value;}templateinlineR

c++ - 部分模板特化的问题

我有以下类结构//filefoo.h:structfoo_base{...}templatestructfoo:foo_base{...};templateusingis_foo=std::is_convertible;templatestructaux;templatestructaux::value>::type>{...};//specialisationforanyfoo//filebar.h:#include"foo.h"templatestructbar:foo{...};templatestructaux>{...};//specialisationforbar现在,问题

c++ - 未实例化模板函数的处理

以下代码可在VisualC++2013中编译,但不能在G++4.8.2下编译:templateintMyFunc(T&t){returnstatic_cast(CCodes::blah);}templateintMyFunc(float&t){return0;}intmain(){floatf=10.f;returnMyFunc(f);}VisualC++似乎忽略了通用模板函数,因为只有特化MyFunc用来。G++无论如何都会解析通用函数并发现CCodes枚举尚未定义。哪个是对的?或者这是实现定义的? 最佳答案 GCC是正确的,除了