草庐IT

TEMPLATE

全部标签

C++ 如何使 template<T>f() 为整数 T 返回 -1,为指针类型返回 nullptr

我需要完成以下任务:templatef(){:return{-1ifTisofintegraltype,elsenullptr}}在我的特定用例中,T可以是四种类型之一:intPy_ssize_t//ssize_tPy_hash_t//ssize_tPyObject*//PyObjectissomeCstruct这是我迄今为止最好的解决方案:templateTtest(typenameenable_if::value,void*>::type=nullptr){return-1;}templateTtest(typenameenable_if::value,void*>::type=n

c++ - C++ 模板如何专用于所有 32 位 POD 类型?

我开发了一个简单的模板函数来交换单个字段的字节顺序:templateinlinevoidSwapEndian(T&ptr){char*bytes=reinterpret_cast(&ptr);inta=sizeof(T)/2;while(a--){chartmp=bytes[a];intb=sizeof(T)-1-a;bytes[a]=bytes[b];bytes[b]=tmp;}}我经常在T=int或float的地方使用它。这两种类型在目标平台上均由4个字节表示,并且可以由模板的相同特化处理。因为这个函数有时负责处理大缓冲区的原始数据,所以我创建了一个优化的特化:templatein

C++ 模板化友元类

我正在尝试用C++编写一个2-3-4树的实现。自从我使用模板以来已经有一段时间了,而且我遇到了一些错误。这是我非常基本的代码框架:节点.h:#ifndefTTFNODE_H#defineTTFNODE_HtemplateclassTreeNode{private:TreeNode();TreeNode(Titem);Tdata[3];TreeNode*child[4];friendclassTwoThreeFourTree;intnodeType;};#endif节点.cpp:#include"node.h"usingnamespacestd;template//defaultcons

c++ - 友元模板函数的正确语法

在TheC++ProgrammingLanguage,FourthEdition-chapter23.4.7Friends中,我找到了以下示例(我对其进行了稍微修改以仅显示相关部分):templateclassVector{public:friendVectoroperator*(constVector&v,intf);^^~~~~?};templateVectoroperator*(constVector&v,intf){returnv;}我试图编译它,但出现以下错误(clang):main.cpp:8:20:error:friendscanonlybeclassesorfuncti

c++ - 访问模板参数 T 的嵌套类型,即使 T 是指针

基本结构:structFoo{typedefintinner_type;};templatestructBar{typenameT::inner_typex;};主要内容:Bar();//CompilesOKBar();//Doesn'tcompile:templateTbecomesapointer-to-classandisnotavalidclassanymore.如何解决这个问题? 最佳答案 将Bar结构特化为指向T类型的指针://non-specializedtemplateforgenerictypeTtemplates

c++ - 在模板中使用 -> 以强制以下符号依赖

来自问题:Properuseofthis->答案是->可以用...inatemplate,inordertoforcethefollowingsymboltobedependent—inthislatteruse,itisoftenunavoidable.这是什么意思?这种用法的一个很好的例子是什么?我不太明白“依赖”在这种情况下是什么意思,但这听起来像是一个有用的技巧。 最佳答案 发表于其他问题:templatestructfoo:T{voidbar(){x=5;//doesn'tworkthis->x=5;//works-Tha

c++ - 与模板特化成为 friend 时可能出现 gcc 错误

在回答关于SO的另一个问题时,我遇到了一个有点可疑的gcc编译器错误。有问题的片段是templateclassA;templatevoidoperator*(A,A);templateclassA{friendvoid::operator*(A,A);...最后一行给出了著名的警告frienddeclaration'voidoperator*(A,A)'declaresanon-templatefunction稍后会导致硬错误。完整代码可见here.现在,问题是我认为这种行为不合适。[temp.friend]/1中的标准说:Forafriendfunctiondeclarationth

c++ - 为什么宏 __STL_FUNCTION_TMPL_PARTIAL_ORDER 应该将模板函数包含在 std_pair.h 中

今天在STL_pair.h中看到如下代码:#ifdef__STL_FUNCTION_TMPL_PARTIAL_ORDERtemplateinlinebooloperator!=(constpair&__x,constpair&__y){return!(__x==__y);}templateinlinebooloperator>(constpair&__x,constpair&__y){return__y我不认为模板函数与偏特化有任何关联的功能模板。我错了吗? 最佳答案 编译器如何处理函数调用在C++中调用函数模板经历了名称查找(标准

c++ - 为什么模板模板参数不允许 'typename'在参数列表之后

模板模板类型名?当使用templatetemplate语法时templateclassT>,需要使用关键字class,作为使用typename给出如下错误:error:templatetemplateparameterrequires'class'aftertheparameterlist其他地方的关键词typename和class在声明模板参数的基本情况下可以互换。你可能会争辩说,使用模板模板时的要求是暗示你应该传递一个类类型,但情况并非总是如此(尤其是在C++11引入模板化之后)类型别名)。templateclassT>//'class'keywordrequired.struct

c++ - 在大模板类中专门化单个方法

在C++中,如果您想部分特化模板类中的单个方法,则必须特化整个类(如Templatespecializationofasinglemethodfromtemplatedclasswithmultipletemplateparameters中所述)然而,当每个模板参数影响单个函数时,这在具有多个模板参数的较大模板类中变得令人厌烦。使用N个参数,您需要将类特化2^N次!然而,对于C++11,我认为可能有更优雅的解决方案,但我不确定如何处理它。也许以某种方式使用enable_if?有什么想法吗? 最佳答案 除了Torsten提出的基于继承