草庐IT

C++ 成员模板特化语法

关于thissite有以下段落:Whendefiningamemberofanexplicitlyspecializedclasstemplateoutsidethebodyoftheclass,thesyntaxtemplateisnotused,exceptifit'samemberofanexplicitlyspecializedmemberclasstemplate,whichisspecializedasaclasstemplate,becauseotherwise,thesyntaxwouldrequiresuchdefinitiontobeginwithtemplater

c++ - 为什么允许嵌套类模板的部分特化,而不允许完全特化?

templatestructA{templatestructB{};.templatestructC{};};templatetemplatestructA::B{};//error:enclosingclasstemplatesarenotexplicitlyspecializedtemplatetemplatestructA::C{};//ok那么,如果外部类也不是特化的,那么为什么不允许对内部嵌套类(或函数)进行显式特化呢?很奇怪,如果我只是部分通过简单地添加一个虚拟模板参数来专门化内部类,我就可以解决这个问题。使事情变得更丑陋和更复杂,但它有效。我会将完全特化视为部分特化的子集

c++ - 非命名空间范围内的显式特化

这个问题在这里已经有了答案:C++syntaxforexplicitspecializationofatemplatefunctioninatemplateclass?(9个回答)关闭8年前。templateclassCConstraint{public:CConstraint(){}virtual~CConstraint(){}templatevoidVerify(intposition,intconstraints[]){}templatevoidVerify(int,int[]){}};在g++下编译会出现以下错误:Explicitspecializationinnon-name

c++ - 整数类型的模板函数特化

假设我有一个模板函数:templatevoidf(Tt){...}我想为所有原始整数类型编写一个特化。执行此操作的最佳方法是什么?我的意思是:template::valueistrue>voidf(Ii){...}然后编译器为整数类型选择第二个版本,为其他所有类型选择第一个版本? 最佳答案 使用SFINAE//Foralltypesexceptintegraltypes:templatetypenamestd::enable_if::value>::typef(Tt){//...}//Forintegraltypesonly:tem

c++ - 模板类中的模板成员特化

#include#includetemplatestructA{templateautofunc();templateautofunc();};templatetemplateautoA::func(){returnstd::string{"foo"};}intmain(){Aa{};std::cout()这不起作用,因为模板类的模板成员的特化是不可能的,除非您也特化该类。(我读到过。)但是如果您将成员特化的定义移动到类定义中,它确实有效:#include#includetemplatestructA{templateautofunc();templateautofunc(){retu

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++ - GCC 4.6.3 - 模板特化受优化级别影响?

在我正在开发的应用程序中,我有一个这样的模板函数:templatevoidCIO::writeln(Titem){stringstreamss;ss这个函数从几个地方调用,T=constchar*和T=std::string。使用CodeSourceryLite2008.03-41(GCC4.3.2),此编译和链接很好,带有-O3编译器标志。但是,由于我更改为CodeSourceryLite2012.03-57(GCC4.6.3),使用-O3进行编译是可以的,但随后链接失败并显示undefinedreferencetovoidCIO::writeln(std::string)。.使用-

c++ - 使用模板特化、默认参数和 VS2013 编译错误

templatevoidf(constT&v=T());templatevoidf(conststd::string&v){std::cout();//ErrorinVS2013,OKinVS2012,gcc-4.7f("Test");//OKf(std::string());//OKreturn0;}对于必须使用默认参数的情况,最新的VisualStudio2013编译器会给出以下编译器错误:errorC2440:'defaultargument':cannotconvertfrom'conststd::string*'to'conststd::string&'Reason:cann

c++ - 任意顺序的多个标签的模板特化

假设我有一个像这样定义的类event_basetemplateclassevent_base{public:usingdelegate_type=std::function;usingid_type=size_t;protected:std::vector>m_funcs;};和一些像这样定义的空标签结构structmutable_tag{};structcallable_tag{};然后我希望能够基于模板包中给定的标签中的一个(或两个)从该基类派生对于单个标签:templateclassevent;templateclassevent:publicevent_base{};templ