草庐IT

template-specialization

全部标签

c++ - "typename"和 "template"关键字 : are they really necessary?

编译c++模板代码时,这个站点上有很多问题。此类问题最常见的解决方案之一是在程序代码的正确位置添加typename(以及不太常见的template)关键字:templateclassBase{public:typedefcharSomeType;templatevoidSomeMethod(SomeType&v){//...}};templateclassDerived:publicBase{public:voidMethod(){typenameBase::SomeTypex;//^^^^^^^^this->templateSomeMethod(x);//^^^^^^^^}};是否有

c++ - 为什么这些 C++ 案例实例化不同的模板

我正在尝试编写一些功能,我需要在其中保存不同的函数,然后提取它们的参数类型。所以我使用函数签名作为模板参数。但是我得到了一些意想不到的结果。这是代码:#include#includetemplatestructfoo{foo(){std::coutstructfoo{foo(){std::coutvoidsave(std::function){newfoo();}intmain(intargc,char*argv[]){std::functionsomeFoo;save(someFoo);return0;}所以如果变量someFoo是类型为void(void)的函数,它实例化第一个模板

c++ - 具有默认模板参数的模板结构未实例化

假设我有这段代码templatestructX{staticdoublef;};templatedoubleX::f=14.0;如果我尝试编译clang会出现以下错误nestednamespecifier'X::'fordeclarationdoesnotreferintoaclass,classtemplateorclasstemplatepartialspecialization对于海湾合作委员会:error:templatedefinitionofnon-template'doubleX::f'问题是:为什么编译器要我们像这样专门化结构X:templatestructX{stat

c++ - 如何选择部分模板特化?

请解释一下模板特化选择的规则。我有一个例子:templatestructS:false_type{};templatestructS:true_type{};cout::value;为什么输出是false?一般来说,在特殊类中默认模板参数typenameT2=int会发生什么?它是否引入了一些影响? 最佳答案 选择模板特化分为五个步骤:采用主模板声明。(S)填写用户指定的模板参数。(T1)仅限函数模板:推导额外的模板参数。对剩余的模板参数使用默认值。(T2)使用偏序算法(C++1414.5.6.2)选择最匹配的特化。(不匹配,所以忽

c++ - 不能明确特化

我收到这个错误:SeverityCodeDescriptionProjectFileLineSuppressionStateErrorC2910'addingStuff::addingStuffFunc':cannotbeexplicitlyspecializedBuckysTemplateSpecializationc:\users\amanuel\documents\visualstudio2015\projects\buckystemplatespecialization\buckystemplatespecializ当我尝试运行这段代码时:#include#includetem

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

我有一个模板函数(为了简单起见,我们称它为“add”)templateinlineTadd(constTa,constTb){returna+b;}我可以针对某些类型专门化它,但我想做的是针对模板化类型专门化它。在我的例子中,我的模板类型叫做Vec2.它是一个二维三角vector(如x和y,而不是c++vector!)我想做的是专门化我的addVec2一般情况下的函数,而不是必须专门针对Vec2的每种类型可以一起使用。Vec2的图书馆来自V2d的类型定义(双),V2f(float)和V2i(整数)。我可以专门针对其中的每一个使用类似的东西:templateinlineV2fadd(co

C++ 模板 : cannot match the last template in variadic class template

我正在学习C++11可变参数模板并创建了一个模板结构来计算给定列表的最大数量并尝试了:#include#includetemplatestructmax:std::integral_constantb?max::value:max::value)>{};templatestructmax:std::integral_constantb?max::value:max::value)>{};templatestructmax:std::integral_constant{};intmain(){std::cout::value但是g++提示:test.cc:7:58:error:wrong

c++ - C++ 11 中具有多个模板参数的模板函数的特化

无法弄清楚为什么我会收到以下代码的编译器错误:#includetypedefTCHARChar;typedefstd::basic_stringString;templatestd::basic_stringInternalToString(Tval);templateinlinestd::stringInternalToString(Tval){returnstd::to_string(val);}templateinlinestd::wstringInternalToString(Tval){returnstd::to_wstring(val);}templateinlineStr

c++ - 如何专门化模板类成员结构

假设我有以下模板类:templateclassFoo{structstore_t{uint8_tdata[];}store;///otherstuffusingT}有没有办法构造一个专门版本的内部结构,相当于这样的东西:classFoo{structstore_t{uint16_tf1;uint16_tf2;}store;///otherstuffusingT}我宁愿让大部分“使用T的其他东西”保持非特化。不过,我会专门研究一些访问器。我觉得我想写一些类似的东西templatestructstore_t{uint16_tf1;uint16_tf2;}Foo::store;但这当然行不通

c++ - 显式特化中函数名称后的空尖括号

这是文章Whynotspecializefunctiontemplates?中的代码templatevoidf(T);//(1)templatevoidf(T*);//(2)templatevoidf(int*);//(3)我的问题是关于最后的声明。该语法是什么意思?当我们想要完全特化一个函数模板时,例如(1)、对于某些类型我们通常这样写:templatevoidf(int);即我们将该类型放入函数名称后的尖括号中。那么语法(3)是什么意思呢? 最佳答案 在你的情况下,templatevoidf(int*);是的显式特化templa