jjzjj

专门化

全部标签

c++ - 使用枚举来专门化模板

我一直在使用带有枚举参数的模板来为我的代码输出提供专门的方法。templatestructsensorOutput;templatestructsensorOutput{voidsetData(Objects&objs){//outputspecifictoDEVICE1//outputvelocityobjs.set(VELOCITY,vel[Device::DEVICE1]);//outputpositionobjs.set(POSITION,pos[Device::DEVICE1]);}};templatestructsensorOutput{voidsetData(){//ou

c++ - Dirent.h - 专门获取具有特定扩展名的文件的字符串

有一些问题试图想出一个好方法来拥有一个只选择wav文件并专门返回这些文件的字符串的目录遍历器,例如只是wav文件。我可以停止dirent.h在子目录中查找,我很确定我只需要一个if语句,它只允许字符串中带有.wav的文件到目前为止我的代码看起来是这样的,我只是扩展了我在头文件的wiki页面上看到的几个例子:ifstreamfin;stringdir,filepath;DIR*dp;structdirent*dirp;structstatfilestat;coutd_name;if(stat(filepath.c_str(),&filestat))continue;if(S_ISDIR(

c++ - 如何专门化模板函数以让它接受将 char 数组作为参数传递?

//problem.cpp:#includetemplatevoidfunc(constT&v);intmain(){inti;floatf;char*cp;charca[4];func(i);func(f);func(cp);func(std::string("std::string"));func(ca);func("string_literal");return0;}//problem2.cpp#includetemplatevoidfunc(constT&v);//undefinedreferenceto`voidfunc(intconst&)'templatevoidfunc

c++ - 从其他专门函数调用专门函数

我有一个错误C2910:'TEMPLATE_TEST::FuncTemplateTest::InnerFunc':cannotbeexplicitlyspecialized,同时编译下面的代码。有两个模板函数,它们都是专门的。当我在专门的外部函数中删除对InnerFunc的调用时,一切正常。那么,问题出在哪里呢?(我使用的是MSVS2008。)classFuncTemplateTest{public:templateconstintOuterFunc(constT&key)const;private:templateconstintInnerFunc(constT&key)const;

c++ - 将模板专门化为嵌套类类型

有没有专门针对嵌套类的特征模板?我已经在下面提到的三个地方试过了,每个地方都有给定的错误。我已经看到有关专门化嵌套模板类的问题,但这不是我在这里尝试做的——我正在尝试专门化嵌套类使用的特征类。TraitUser类使用Trait中的定义作为特定类型T的特化。也许最相关的是,它使用trait成员来初始化基类。templateclassTraitUser:publicX::Type>{//Traitgetsusedinhere};//classA;//classA::B;//structTrait//{};classA{private://classB;//template//{};clas

c++ - 为派生类专门化 std::hash 在 gcc 中工作,而不是 clang

我正在尝试为派生类专门化std::hash。目前最好的方法是基于thisanswer:#include#include#includenamespacefoo{templateusingfirst=T;structhashable{};structbar:publichashable{};}namespacestd{templatestructhash::value>>>{size_toperator()(constT&x)const{return13;}};}intmain(){std::unordered_setbaz;return0;}使用g++5.2.0编译时没有警告(-Wal

c++ - 当 C++ 中也有非模板函数时,调用专门的模板函数

在这个例子中,怎么可能调用第二个函数呢?templateTsquare(Ta){std::coutintsquare(inta){std::cout提前致谢! 最佳答案 通过显式指定模板参数调用特化:square(2);LiveDemo 关于c++-当C++中也有非模板函数时,调用专门的模板函数,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/36176398/

c++ - 如何在 C++ 中用枚举专门化函数的返回类型?

我正在使用一个变体来为C++语法分析器存储一系列类型。语法规则的每个组成部分都有一个类别(枚举类型)和一个值。成分根据类别存储一种类型的值。为了举例,我将类别简化为“String”=>存储一个字符串,“Number”=>存储一个int。我想根据其类别枚举获取具有正确类型的成分的值。我该怎么做?我在下面编写了示例代码,其中我构造了两个组成部分:strCon,存储一个字符串,以及intCon,存储一个int,并尝试获取它们的值。我想把strCon中的字符串赋值给strVal,以及从intCon到intVal的int。#includestructConstituent{enumclassCa

c++ - 表达式模板 - 无法专门化函数模板

我试图制作一些表达式模板作为对thisquestion的回答,但我遇到了编译器错误,我无法弄清楚。到目前为止,我已经获得了相当小的SSCCEtemplateclassinherit2:privatesub_expr{//line3public:inherit2(sub_exprrhs):sub_expr(rhs){}templateautooperator()(constT&v)const->decltype(sub_expr::operator()(v))//line7{returnsub_expr::operator()(v);}};classexpression_paramete

c++ - 内联导致覆盖虚函数的模板类的专门成员函数被忽略

我想和你们分享一个我偶然发现的奇怪的例子,这让我思考了两天。要让这个例子正常工作,您需要:三角形虚继承(成员函数getAsString())模板类的成员函数特化(此处为Value::getAsString())覆盖虚函数(自动)由编译器内联你从一个模板类开始,它实际上继承了一个公共(public)接口(interface)——即一组虚函数。稍后,我们将特化其中一个虚函数。内联可能会导致我们的特化被忽视。//test1.cppandtest2.cpp#includeclassValueInterface_common{public:virtual~ValueInterface_commo