草庐IT

Specialization

全部标签

c++ - 没有参数的函数的模板特化

我需要在c++中专门化一个函数模板。templatevoiddoStuff(){}到templatevoiddoStuff();和templatevoiddoStuff();我猜这不是正确的语法(因为它没有编译)。我该怎么做呢?另外,由于我在doStuff中没有未定义的模板参数,是否可以在.cpp中声明正文?注意:doStuff将在其主体中使用T来声明变量。 最佳答案 主模板没有获得第二对模板参数。就是这样:templatevoiddoStuff(){}//^^^^^^^^^只有专精具有template在前面和在名称之后,例如:te

c++ - 编译时检查 trait specialization 是否有唯一的 id

我看过很多解释如何为类生成唯一ID的帖子。在我的例子中,id是由用户选择的(出于各种原因),但我想确保没有id在不同的类中被使用两次。我将我的问题简化为以下代码:structA{};structB{};templatestructtraits{};templatestructtraits{staticconstexprsize_tid(){return0;}}templatestructtraits{staticconstexprsize_tid(){return1;}}现在,有没有一种简单的方法可以确保有人不会添加具有重复id的特性的特化:structC{};templatestru

c++ - 错误 : class template partial specialization contains a template parameter that cannot be deduced

我很感激帮助弄清楚我的代码中出现的这个问题是怎么回事,我已将其简化为以下内容:typedefunsignedshortushort;templatestructFoo{};//Specialization--workswhennotaspecializationtemplateclassContainer,templateclass>classMetaFunction>structFoo::Type>>{//typedefContainer::Type>TestType;//OK};intmain(){}在编译(gcc5.4.0)时出现错误:Test.cpp:14:8:error:te

c++ - Template Explicit Specialization 和普通函数有什么区别?

templatevoidmax(T&a,T&b){}//generictemplate#1templatevoidmax(char&c,char&d){}//templatespecializtion#2voidmax(char&c,char&d){}//ordinaryfunction#31、2、3有什么区别? 最佳答案 是一个模板函数是之前模板函数的完全特化(不重载!)是函数的重载这是来自C++CodingStandards:101Rules,Guidelines,andBestPractices的摘录:66)Don'tspec

C++1y/C++14 : Variable Template Specialization?

根据C++1y/C++14N3690,变量模板特化的类型是否必须与主模板的类型相同?templatechary=f(x);templatedoubley=g();如果是这样,是否有可能以某种方式使主要的未定义?template????y=???;//undefinedtemplatedoubley=g();草案中的哪些内容?类模板的等效功能是:templatestructS{staticchary;};templatestructS{staticdoubley;};和templatestructS;//undefinedtemplatestructS{staticdoubley;};

C++:嵌套模板类错误 "explicit specialization in non-namespace scope"

以下代码:templatestructA1{templatestructA2{/*...*/};templatestructA2{/*...*/};};intmain(){A1::A2x;}给出这个错误:prog.cpp:7:13:error:explicitspecializationinnon-namespacescope'structA1'prog.cpp:8:10:error:templateparametersnotusedinpartialspecialization:prog.cpp:8:10:error:'T1'如何最好地解决此错误?我试过这个:templatestru

C++:嵌套模板类错误 "explicit specialization in non-namespace scope"

以下代码:templatestructA1{templatestructA2{/*...*/};templatestructA2{/*...*/};};intmain(){A1::A2x;}给出这个错误:prog.cpp:7:13:error:explicitspecializationinnon-namespacescope'structA1'prog.cpp:8:10:error:templateparametersnotusedinpartialspecialization:prog.cpp:8:10:error:'T1'如何最好地解决此错误?我试过这个:templatestru

c++类模板特化,无需重新实现一切

我有一个这样的模板化类:templateclassA{protected:std::vectormyVector;public:/*constructors+abunchofmemberfunctionshere*/}我只想添加一个仅适用于1种给定类型T的成员函数。是否可以完全做到这一点而无需专门化该类并重新实现所有其他已经存在的方法?谢谢 最佳答案 最简单、最干净的解决方案是使用static_assert()在方法的主体中,拒绝所选类型以外的其他类型(在下面的示例中,仅接受整数):#include#includetemplatec

c++类模板特化,无需重新实现一切

我有一个这样的模板化类:templateclassA{protected:std::vectormyVector;public:/*constructors+abunchofmemberfunctionshere*/}我只想添加一个仅适用于1种给定类型T的成员函数。是否可以完全做到这一点而无需专门化该类并重新实现所有其他已经存在的方法?谢谢 最佳答案 最简单、最干净的解决方案是使用static_assert()在方法的主体中,拒绝所选类型以外的其他类型(在下面的示例中,仅接受整数):#include#includetemplatec

c++ - GCC 错误 : explicit specialization in non-namespace scope

我正在尝试移植以下代码。我知道标准不允许在非名称范围范围内进行显式特化,我应该使用重载,但我只是找不到在这种特殊情况下应用这种技术的方法。classVarData{public:templateboolIsTypeOf(intindex)const{returnIsTypeOf_f::IsTypeOf(this,index);//noerror...}templateboolIsTypeOf(intindex)const//error:explicitspecializationinnon-namespacescope'classStateData'{returnfalse;}temp