c++模板c9 疑问箱子

一个C++关于模板的特例化的问题:【c++吧】_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:302,361贴子:
一个C++关于模板的特例化的问题:收藏
我这里定义了一个类模板compare,当主函数中调用时其数据类型是const int 类型,但是还是调用的是普通的模板,没有调用我那个特例化的const int的模板,求大神解答,下面是源码:#include &iostream&template&typename T&bool compare(T a,T b){cout && &template compare& &&return a &}template&&bool compare&const int&(const int a, const int b)
//compare&int&{cout && &template&const int& compare& &&return a &}int main(){const int a = 10;const int b = 20;compare(a,b); return 0;}
类模板。函数模板参数当 pass by val 的时候推导出来的类型没有 const。
把const去掉
我只是想知道如何才能使用上特例化的const int 的模板,就是会打印“template&const int& compare”
我希望编译器使用特例化的模板,我将其改为传引用:bool compare&const int&&(const int &a, const int &b)
{cout && &template&const int& compare& &&return a &}当我把特例化的模板改成这样,他为什么仍然不调用这个呢,他已经不是按值传递了啊
模板特化不参与类型推导/重载决议
登录百度帐号已解决问题
新手C++问题:有关template specialization的定义问题
定义template && FuncName(&&&&)之中的那个实在不明白有什么用处。如果是想区分template function和template spacialization的区别,那么template后面的空尖括号&&也足够了;如果是想说明这个specialization是特定对于Type数据类型的,那么圆括号( )里面的东西不是也足够了吗?书中的例子基本上都是template && Swap(box a, box b)
(其中box是个structure)。因为圆括号里面的数据都是box,所以& &里面写box也可以理解;但是如果圆括号里面两个参数一个是box,一个是int,那么尖括号里面该写什么呢?弱智问题,希望高手解答!PS:还有一个小小的概念问题:数据转换当中的standard conversion和promotion分别指什么意思啊?谢谢!
浏览次数:408
用手机阿里扫一扫
最满意答案
圆括号里面的是函数的参数,尖括号的是模板的类型参数比如一个模板函数是swap&T&(T&t1,&T&t2)&那么其中的T就是模板参数,对于box类,如果你的swap函数需要特殊处理,你就定制一个swap&box&(box&b1,&box&b2)函数,尖括号中的box是告诉编译器,这个函数是为了swap&T&定制的,其中T为box类因为你要为swap函数定制的类型T是box,所以参数类型自然是把T替换成box,你写成box,int就不能编译了。你说圆括号(&)里面的东西不是也足够了吗?不知道你的意思是不是指只要写了(box,&box),编译器能自动推测出你想要为模板specialize的类型。如果你是这个意思,那么我觉得c++的类型体系足够复杂,使得编译器无法这么做,会产生歧义。具体例子暂时不举了另外提一点,如果你这么写swap(box,box),编译器会认为这是一个普通的函数,而不是某个模板函数的一个specialization&&&&&&&&&&&&&&&&&&&&&&&&&&&-standard&conversion和promotion这个问题,你突然这么提两个概念,我也不确定。希望你给出他们在你看的书里面的出处。&从字面意思来看,promote似乎是指int到long或者double之类的转换,这种转换属于standard&conversion的一种。这是我的猜想
答案创立者
以企业身份回答&
正在进行的活动
生意经不允许发广告,违者直接删除
复制问题或回答,一经发现,拉黑7天
快速解决你的电商难题
店铺优化排查提升2倍流量
擅长&nbsp 店铺优化
您可能有同感的问题
扫一扫用手机阿里看生意经
问题排行榜
当前问题的答案已经被保护,只有知县(三级)以上的用户可以编辑!写下您的建议,管理员会及时与您联络!
server is okC++类模板的特性_百度经验
&&&&&&&&&电脑软件C++类模板的特性听语音123
百度经验:jingyan.baidu.comC++类模板的特征决定了你在编程时的抉择和考虑,具体已经介绍在每一个特征里,希望能帮助大家。百度经验:jingyan.baidu.com1特化为绝对类型
也就是说直接为某个特定类型做特化,这是我们最常见的一种特化方式,如特化为float,double等
// specialize for floattemplate&&class Compare&float&{public:& static bool IsEqual(const float& lh, const float& rh)& {& & & return abs(lh - rh) & 10e-3;& }};// specialize for doubletemplate&&class Compare&double&{public:& static bool IsEqual(const double& lh, const double& rh)& {& & & return abs(lh - rh) & 10e-6;& }};
2特化为引用,指针类型
这种特化我最初是在stl源码的的iterator_traits特化中发现的,如下:
template &class _Iterator&struct iterator_traits {typedef typename _Iterator::iterator_category iterator_typedef typename _Iterator::value_type & & & &value_typedef typename _Iterator::difference_type & difference_typedef typename _Iterator::pointer & & & & &typedef typename _Iterator::reference & & & &};
// specialize for _Tp*template &class _Tp&struct iterator_traits&_Tp*& {typedef random_access_iterator_tag iterator_typedef _Tp & & & & & & & & & & & & value_typedef ptrdiff_t & & & & & & & & & difference_typedef _Tp* & & & & & & & & & & & &typedef _Tp& & & & & & & & & & & & &};// specialize for const _Tp*template &class _Tp&struct iterator_traits&const _Tp*& {typedef random_access_iterator_tag iterator_typedef _Tp & & & & & & & & & & & & value_typedef ptrdiff_t & & & & & & & & & difference_typedef const _Tp* & & & & & & & & &typedef const _Tp& & & & & & & & & &};当然,除了T*,我们也可以将T特化为constT*,T&,constT&等,以下还是以T*为例:
// specialize for T*template&class T&class Compare&T*&{public:&static bool IsEqual(const T* lh, const T* rh)&{& & &return Compare&T&::IsEqual(*lh, *rh);&}};这种特化其实是就不是一种绝对的特化,它只是对类型做了某些限定,但仍然保留了其一定的模板性,这种特化给我们提供了极大的方便,如这里,我们就不需要对int*,float*,double*等等类型分别做特化了。
3特化为另外一个模板类
这其实是第二种方式的扩展,其实也是对类型做了某种限定,而不是绝对化为某个具体类型,如下:
// specialize for vector&T&template&class T&class Compare&vector&T& &{public:& &static bool IsEqual(const vector&T&& lh, const vector&T&& rh)& &{& & & &if(lh.size() != rh.size())& & & &else& & & &{& & & & & &for(int i = 0; i & lh.size(); ++i)& & & & & &{& & & & & & & &if(lh[i] != rh[i])& & & & & &}& & & &}& & & && &}};这就把IsEqual的参数限定为一种vector类型,但具体是vector&int&还是vector&float&,我们可以不关心,因为对于这两种类型,我们的处理方式是一样的,我们可以把这种方式称为“半特化”。
END经验内容仅供参考,如果您需解决具体问题(尤其法律、医学等领域),建议您详细咨询相关领域专业人士。作者声明:本篇经验系本人依照真实经历原创,未经许可,谢绝转载。投票(0)已投票(0)有得(0)我有疑问(0)◆◆说说为什么给这篇经验投票吧!我为什么投票...你还可以输入500字◆◆只有签约作者及以上等级才可发有得&你还可以输入1000字◆◆如对这篇经验有疑问,可反馈给作者,经验作者会尽力为您解决!你还可以输入500字相关经验00700热门杂志第1期你不知道的iPad技巧3740次分享第1期win7电脑那些事6607次分享第2期新人玩转百度经验1395次分享第1期Win8.1实用小技巧2644次分享第1期小白装大神1903次分享◆请扫描分享到朋友圈C++模板SFINAE疑问? - 知乎14被浏览453分享邀请回答01 条评论分享收藏感谢收起c++函数模板匹配的问题? - 知乎39被浏览<strong class="NumberBoard-itemValue" title="分享邀请回答#1
clang::compareStandardConversionSubsets (Context=..., SCS1=..., SCS2=...)
at llvm-3.4.2.src/tools/clang/lib/Sema/SemaOverload.cpp:3393
0x00007ffff6cfb353 in clang::CompareStandardConversionSequences (S=..., SCS1=..., SCS2=...)
at llvm-3.4.2.src/tools/clang/lib/Sema/SemaOverload.cpp:3469
0x00007ffff6cfaeff in clang::CompareImplicitConversionSequences (S=..., ICS1=..., ICS2=...)
at llvm-3.4.2.src/tools/clang/lib/Sema/SemaOverload.cpp:3336
0x00007ffff6d0ac37 in clang::isBetterOverloadCandidate (S=..., Cand1=..., Cand2=..., Loc=...,
UserDefinedConversion=false)
at llvm-3.4.2.src/tools/clang/lib/Sema/SemaOverload.cpp:8031
0x00007ffff6d0afd1 in clang::OverloadCandidateSet::BestViableFunction (this=0x7fffffff77a0, S=...,
Loc=..., Best=@0x7ffffffffffffff7860, UserDefinedConversion=false)
at llvm-3.4.2.src/tools/clang/lib/Sema/SemaOverload.cpp:8148
0x00007ffff6d12220 in clang::Sema::BuildOverloadedCallExpr (this=0x7445e0, S=0x781630, Fn=0x782620,
ULE=0x782620, LParenLoc=..., Args=..., RParenLoc=..., ExecConfig=0x0, AllowTypoCorrection=true)
at llvm-3.4.2.src/tools/clang/lib/Sema/SemaOverload.cpp:10394
0x00007ffff6bceb9a in clang::Sema::ActOnCallExpr (this=0x7445e0, S=0x781630, Fn=0x782620, LParenLoc=...,
ArgExprs=..., RParenLoc=..., ExecConfig=0x0, IsExecConfig=false)
at llvm-3.4.2.src/tools/clang/lib/Sema/SemaExpr.cpp:4470
0x00007ffff7255459 in clang::Parser::ParsePostfixExpressionSuffix (this=0x75f6f0, LHS=...)
at llvm-3.4.2.src/tools/clang/lib/Parse/ParseExpr.cpp:1455
0x00007ffff7254925 in clang::Parser::ParseCastExpression (this=0x75f6f0, isUnaryExpression=false,
isAddressOfOperand=false, NotCastExpr=@0x7fffffffa59f: false, isTypeCast=clang::Parser::NotTypeCast)
at llvm-3.4.2.src/tools/clang/lib/Parse/ParseExpr.cpp:1279
0x00007ffff7251fba in clang::Parser::ParseCastExpression (this=0x75f6f0, isUnaryExpression=false,
isAddressOfOperand=false, isTypeCast=clang::Parser::NotTypeCast)
at llvm-3.4.2.src/tools/clang/lib/Parse/ParseExpr.cpp:419
#10 0x00007ffff7251105 in clang::Parser::ParseAssignmentExpression (this=0x75f6f0,
isTypeCast=clang::Parser::NotTypeCast)
at llvm-3.4.2.src/tools/clang/lib/Parse/ParseExpr.cpp:168
#11 0x00007ffff7250f2c in clang::Parser::ParseExpression (this=0x75f6f0, isTypeCast=clang::Parser::NotTypeCast)
at llvm-3.4.2.src/tools/clang/lib/Parse/ParseExpr.cpp:120
#12 0x00007ffff727ba85 in clang::Parser::ParseExprStatement (this=0x75f6f0)
at llvm-3.4.2.src/tools/clang/lib/Parse/ParseStmt.cpp:371
#13 0x00007ffff727b46b in clang::Parser::ParseStatementOrDeclarationAfterAttributes (this=0x75f6f0, Stmts=...,
OnlyStatement=false, TrailingElseLoc=0x0, Attrs=...)
at llvm-3.4.2.src/tools/clang/lib/Parse/ParseStmt.cpp:231
#14 0x00007ffff727abae in clang::Parser::ParseStatementOrDeclaration (this=0x75f6f0, Stmts=...,
OnlyStatement=false, TrailingElseLoc=0x0)
at llvm-3.4.2.src/tools/clang/lib/Parse/ParseStmt.cpp:118
#15 0x00007ffff727d7c8 in clang::Parser::ParseCompoundStatementBody (this=0x75f6f0, isStmtExpr=false)
at llvm-3.4.2.src/tools/clang/lib/Parse/ParseStmt.cpp:907
#16 0x00007ffff7283373 in clang::Parser::ParseFunctionStatementBody (this=0x75f6f0, Decl=0x782340,
BodyScope=...)
at llvm-3.4.2.src/tools/clang/lib/Parse/ParseStmt.cpp:2458
#17 0x00007ffff7223ada in clang::Parser::ParseFunctionDefinition (this=0x75f6f0, D=..., TemplateInfo=...,
LateParsedAttrs=0x7fffffffb8f0)
at llvm-3.4.2.src/tools/clang/lib/Parse/Parser.cpp:1171
#18 0x00007ffff7230a62 in clang::Parser::ParseDeclGroup (this=0x75f6f0, DS=..., Context=0,
AllowFunctionDefinitions=true, DeclEnd=0x0, FRI=0x0)
at llvm-3.4.2.src/tools/clang/lib/Parse/ParseDecl.cpp:1617
#19 0x00007ffff7222b6b in clang::Parser::ParseDeclOrFunctionDefInternal (this=0x75f6f0, attrs=..., DS=...,
AS=clang::AS_none)
at llvm-3.4.2.src/tools/clang/lib/Parse/Parser.cpp:932
#20 0x00007ffff7222c33 in clang::Parser::ParseDeclarationOrFunctionDefinition (this=0x75f6f0, attrs=...,
DS=0x0, AS=clang::AS_none)
at llvm-3.4.2.src/tools/clang/lib/Parse/Parser.cpp:948
#21 0x00007ffff72223bb in clang::Parser::ParseExternalDeclaration (this=0x75f6f0, attrs=..., DS=0x0)
at llvm-3.4.2.src/tools/clang/lib/Parse/Parser.cpp:807
#22 0x00007ffff72218ab in clang::Parser::ParseTopLevelDecl (this=0x75f6f0, Result=...)
at llvm-3.4.2.src/tools/clang/lib/Parse/Parser.cpp:612
#23 0x00007ffff721e1e5 in clang::ParseAST (S=..., PrintStats=false, SkipFunctionBodies=false)
at llvm-3.4.2.src/tools/clang/lib/Parse/ParseAST.cpp:144
103 条评论分享收藏感谢收起2添加评论分享收藏感谢收起写回答

我要回帖

更多关于 c9 疑问箱子 的文章

 

随机推荐