草庐IT

Templates

全部标签

C++模板函数类型推导

我有这个代码:templateclasscallfn{public:voidoperator()(T*obj)const{f(obj);}};voidcall(int*foo){}voidtest(){callfnfun;fun(1);}这往往工作正常。然而,类型callfn到处都在使用,如果我能这样调用它,我会更喜欢callfnfun;相反,在不修改call类型的情况下,是否可以安排callfn模板,使其能够推断出T类型来自f? 最佳答案 在aaronman之上添加的post.尽管您不能单独使用模板类,但使用一些帮助程序(包括宏)

c++ - 检查成员是否存在并调用它或什么也不做

我有一个带有init()方法的模板类,如果它存在,则必须调用子类方法。基类的方法init()永远调用。templateclassBase{templatevoidinit(Args...args);Tsubj;explicitBase(){subj=newT();}}templateBase::init(Args...args){invoke_if_exists(args);//需要实现invoke_if_exists模板。算法应该是这样的代码if(method_exists(T::init)){subj->init(Args...);}我需要将它包装到模板中非常感谢。[更新]:让我尝

c++ - 将模板化回调函数传递给另一个模板化函数

我试图将我的回调函数comp传递给我的模板函数quickSortR但出现以下错误:2IntelliSense:没有函数模板“quickSortR”的实例,对应于参数列表当我取消注释顶部的代码时,我得到了另一个错误列表。所以,我认为问题出在我的回调函数的错误使用上。#include/*templateintcomp(constvoid*,constvoid*);templatevoidquickSortR(T*a,longN,intcomp(constvoid*,constvoid*));Hereismyinstance.Icommentedit.*/templateintcomp(co

C++ 递归模板特化

我写了一个抽象容器模板类,它应该定义数字运算符(一元+和-、二元+、-和*),如果它对模板参数有意义(也就是说,如果它是数字类型)。然后,我想将这些数字操作应用于数值容器的容器(以及数值容器的容器等)。我写了下面的代码。(A)标记显示了我如何尝试解决递归特化问题。templatestructis_numeric:publicstd::is_arithmetic{};template/*(A)*/structis_numeric>:publicstd::is_arithmetic{};/*Classicgenericcontainerfornon-numericbasetypes*/te

c++ - 不接受静态成员函数作为 constexpr 参数

以下代码被clang接受并被gcc拒绝。我想知道这是一个错误还是我遗漏了什么:#includetemplatestaticconstexprTApply(Tin,Tfun(T)){returnfun(in);}templatestructTriangle{usingAr=std::array;staticconstexprArfoo(Arline){returnline;}staticconstexprArresults=Apply({{1}},foo);//foo({1});isok};templateconstexprstd::arrayTriangle::results;intm

c++ - 如何用继承或模板(或其他任何东西?)

我的问题类似于:Can'tusemacrodefineclassinC++,但有点复杂:classABC{public:DECLARATION(ABC)private:voidABCFun1();voidABCFun2();//...andsoon}#defineDECLARATION(TYPE)\std::stringGetClassName()\{\returnstd::string(#TYPE);}\//themacrocangoesontodeclaremore//commoninterfaces,likeInitialize(),...etc.所以,重点是,我可以使用这个宏来

c++ - 此代码示例中使用了哪种模板特化?

我已阅读有关ExplicitSpecializationofClassTemplates的文档和PartialSpecializationofClassTemplates,但不明白这个例子中使用了什么样的特化(msdn链接仅由于我当前的环境而使用,问题或多或少是理论性的)。我需要c++标准中使用的名称和/或文档链接或对c++标准段落的引用。我试图解决的问题非常复杂,无法直接询问,但我知道如何使用与此示例中使用的方法类似的方法。templatestructis_vector{staticboolconstvalue=false;};templatestructis_vector>{sta

C++ 可变参数模板在虚拟抽象的外部未解析

今天我为我的项目编写代码,并在链接器外部遇到Unresolved问题,代码必须生成具有多个虚拟抽象方法的类-作为类集合的基础。所以我决定为此任务使用可变参数模板-但出现了错误。templatestructpin_tag{};//inputstemplateclassinputs_base:publicinputs_base{protected:typedefinputs_basebase_type;usingarg_type=T0;//usingbase_type::_in;virtualvoid_in(T0const&t,pin_tag)=0;};templateclassinput

c++ - 使用可变参数模板指定友元类

我正在尝试使用可变参数模板来指定友元类。我尝试使用以下语法,但它不起作用。templatestructA{friendArgs...;};我尝试编写一些解决方法,但似乎并不那么简单,因为友元不是传递和继承的。所以问题是是否有正确的语法或任何变通方法可以使Args中的每个单独类成为A的友元? 最佳答案 也许以下CRTP变体足以满足您的使用:templateclassAbase{friendArg;virtualintfoo(int)=0;//thisistheprivateinterfaceyouwanttoaccesspublic:

c++ - 将左值传递给 RValue 的参数

我想知道这怎么可能?templatevoidTest(T&&arg){arg=14;}inta=23;Test(a);我的问题是函数Test需要一个右值类型的参数,但它似乎也接受左值类型的参数。这是为什么?那是因为模板的存在吗?因为如果我做这样的事情voidAnotherTest(int&&arg){arg=14;}那么函数要求参数是右值类型。如果有人能解释为什么模板的存在会改变行为,我将不胜感激。 最佳答案 正如您正确想象的那样,关键是它是一个模板并且参数类型被推导。当您使用左值调用Test时,参数类型推导规则当参数是右值引用时将