草庐IT

template-specialization

全部标签

c++ - `template` 关键字限定符能否使代码编译成功,但有所不同?

我正在阅读有关模板关键字限定符(https://www.ibm.com/support/knowledgecenter/SSPSQF_9.0.0/com.ibm.xlcpp111.aix.doc/language_ref/keyword_template_qualifier.html和WhereandwhydoIhavetoputthe"template"and"typename"keywords?)的信息,但仍有一些内容让我感到困惑。是否可能有这样的代码,它编译成功,但会导致两种不同的操作?SomeObjectInstance.templatesome_function();Some

c++ - 如何使用另一个类作为类模板特化

我有一个混合锁类,它在回退到阻塞std::mutex之前尝试锁(编译时固定)自旋数,直到锁可用。简化:#includetemplateclasshybrid_lock{public:voidlock(){for(unsignedi(0);imMutex.try_lock()){return;}}this->mMutex.lock();}voidunlock(){this->mMutex.unlock();}private:std::mutexmMutex;};在SPIN_LIMIT==0的特殊情况下,这会退回到“普通”std::mutex(即没有可见旋转)。所以我将其专门用于:temp

c++ - Clang 无法在模板类特化中编译模板函数,该函数具有来自模板声明的*不同的返回类型*

以下函数derefItemX()在GCC4.8-5.3上编译正常,但在CLang3.8上编译失败://!AccessoryOperations-templateargumentdependedwrapperstemplate//ForNodes/non-scopedstoragestructOperations{//!\briefDefererencewrappedordirectiterator//!//!\paramielIItemXT&-iteratortobedereferenced//!\returnItemT&-resultingreferencetemplateconst

c++ - 根据模板参数添加成员函数和成员变量

我有一个函数族{f_n}其中f_0是连续的,f_1是连续可微的,$f_{n}\inC^{n}[a,b]$等等。我有一个C++类,它通过vectorv上的查找表对f_n进行数值评估templateclassf{public:f(){/*initializev*/}Realoperator()(Realx){/*findappropriateindexforx,andinterpolate*/}private:std::vectorv;};但是,如果f是可微的(n>=1),我想添加一个成员函数:templateclassf{public:f(){/*initializevanddv*/}R

C++ 模板非类型参数算法

我正在尝试通过以下方式专门化模板:template//workaround:boolconsecutive=(_1==_2-1)>structintegral_index_{};...templatestructintegral_index_{//cannotdoarithmetic?//structintegral_index_{workaround};但是我收到编译器消息错误thetemplateargumentlistofthepartialspecializationincludesanon-typeargumentwhosetypedependsonatemplatepara

c++ - 满足特定条件时如何防止 C++ 模板类方法的实例化?

我目前正在编写具有以下签名的通用vector模板类(几何实体,而不是容器)...templateclassvector{...}...其中T是算术类型,N是维度。我想将叉积定义为运算符^的重载(位于类定义内)并仅在N==3时启用它。我现在拥有的是:typenameboost::lazy_enable_if_c::typeinlineoperator^(constvector&rhs)const{vectorret;ret(0)=val_[1]*rhs(2)-val_[2]*rhs(1);ret(1)=val_[2]*rhs(0)-val_[0]*rhs(2);ret(2)=val_[0

c++ - 参数值相等时的模板特化

我有一个表单的函数templatevoidf();当a==b时我想专攻。伪代码看起来像:templatevoidf(){//something}templatevoidf(){//somethingdifferent}如果没有部分模板特化问题,这可能吗?编辑:感谢您的快速回复。该方法实际上在类内部,更像这样:classA{templatef();};Ainst;inst.f();inst.f(); 最佳答案 你不能部分特化一个函数模板,但是你可以部分特化一个类模板。这为以下技巧留下了空间,其中实际工作由主要函数模板和专用函数模板的静

c++ - 如何使用模板模板参数专门化模板

最后编辑我有一个采用模板的函数:templateclassP,typename...Args>voidf(constP&p){std::cout它适用于我目前测试过的任何类型的模板:f(std::valarray{});//Prints:"Templatewith1parameters!"f(std::pair{});//Prints:"Templatewith2parameters!"f(std::set{});//Prints:"Templatewith3parameters!"f(std::map{});//Prints:"Templatewith4parameters!"但是,

c++ - 模板函数中依赖于类型的常量

我想要一个模板化函数中的静态数组,其长度取决于函数专门化的类型。我的第一次尝试是:标题:templatestructLength{conststaticsize_tlen;};templatevoidfunc(){staticTvars[Length::len];//lennotconst.accordingtocompiler!//...}源文件:templateconstsize_tLength::len=2;templateconstsize_tLength::len=1;//...但是,g++不编译它并提示error:storagesizeof‘vars’isn’tconsta

c++ - 如何在函数模板的显式特化中推导模板参数?

考虑以下情况:#includetemplatevoidf(T){std::coutvoidf(constint){std::cout(1);//call#1f(1);//call#2return0;}#2似乎是f(constint)而不是f(constint).这里发生了什么?我的第一个想法是顶级const在函数类型转换中被丢弃,所以#2的类型是void(int),这导致了f(constint)的特化.但我不确定。为什么C++允许这样的语法?我的意思是因为我们不能部分特化函数模板,如果我们想显式特化一个模板参数值,我们就会知道。那么,为什么C++不只是强制程序员在专门化模板函数时显式提