草庐IT

c++ - 一组类型的模板特化

如何为一组数据类型专门化模板?例如:templateinlineTgetRatio(Tnumer,Tdenom){return(numer/denom);}我想专门针对一组数据类型,因此它只适用于int、long、double和float。因此,如果用户尝试将此函数与char类型一起使用,编译器将抛出错误。 最佳答案 这取决于你想做什么。如果您希望编译器无法为函数调用找到合适的解决方案,您可以使用Flinsch的答案,这可能是最好的,或者您可以使用SFINAE:templateis_an_ok_type:boost::mpl::fa

c++ - 带模板的 typedef

templatestructA{typedefintInt;A::Intb;//Line1(fails)Intc;//Line2(compiles)};intmain(){Ax;x.c=13;}错误error:ISOC++forbidsdeclarationof‘Int’withnotypeerror:extraqualification‘A::’onmember‘Int’error:expected‘;’before‘b’第1行失败但第2行编译。为什么? 最佳答案 你需要一个typenametypenameA::Intb;type

c++ - 简单的可变参数模板函数无法实例化

我知道sizeof...(Args...)生成C++0x打包模板参数列表中的类型数,但出于演示目的,我想根据其他功能来实现它,但它无法编译。//Thisisnotasolution--overloadambiguity.//templatesize_tnum_args();//Line7//templateconstexprsize_tnum_args(){return0;}templateconstexprsize_tnum_args()//Line16{return1+num_args();//*HERE*}intmain(){std::cout();}此错误位于*HERE*与No

c++ - C++ 中的模板特化

我一直在努力理解模板特化。为什么这会产生错误(实例化后“Tfoo(T,T)[withT=int]”的特化)templateTfoo(Ta,Tb);intmain(){intx=34,y=54;coutTfoo(Ta,Tb){returna+b;}templateintfoo(inta,intb){cout 最佳答案 标准要求在实例化时必须知道所有模板定义,并且每个翻译单元都看到相同的定义。否则你的程序是错误的(事实上不需要诊断)。(所以要解决这个问题,只需将所有模板定义放在程序的顶部即可。)请记住,模板函数不是函数,只是模板。将它们

c++ - 如何最好地实现具有相互依赖的类型的模板类

作为一个简化的例子,如果我有类templateclassProcessEvent{public:ProcessEvent(T*t):var1(t){var2=newU;}Process(){var2->Process(var1);}private:T*var1;U*var2;};classFoo{/*data*/};classFooProcessor{voidProcess(Foo*foo){/*functionality*/}};classBar{/*data*/};classBarProcessor{voidProcess(Bar*bar){/*functionality*/}};

C++ 模板复制构造函数,编译器说 "passing const as this argument discards qualifiers"

我正在尝试创建动态矩阵的模板类。凭借我目前对C++的了解,我设法解决了一些问题,但我被复制构造函数和重载operator=;困住了。换句话说,我无法创建对象的拷贝。在我看来这应该可行,但我的编译器friend告诉我有1个错误:错误:将“constMatrix”作为“intMatrix::getElement(int,int)[withT=int]”的“this”参数传递会丢弃此行的限定符[-fpermissive]:m[i][j]=original.getElement(i,j);当我想创建一个对象时:Matrixm=Matrix(3,3);我的模板类在这里:templateclass

c++ - 模板模式匹配

考虑这个类模板:templateclassSomeClass{};现在,我想提供两个基于B==true和B==false的实现。也就是说,我想说的是:templateclassSomeClass{//Firstimplementation};templateclassSomeClass{//Secondimplementation};这如何在C++(11)中完成? 最佳答案 部分特化://primarytemplatestructFoo;templatestructFoo{};templatestructFoo{};//useFoo

以非专用模板作为值的 C++ hash_map

我想要一个std::hash_map将(例如)常规std:string映射到多个不同的特化另一个模板类。这个例子是我想要实现的(虽然它是错误的并且无法编译):templateclassFoo{public:Foo(T_value){this->value=_value;}private:Tvalue;};intmain(){hash_mapvarious_foos;various_foos["foo"]=Foo(17);various_foos["bar"]=Foo(17.4);} 最佳答案 map只能存储单一的值类型,所以不能直接

c++ - C++ : "expected primary-expression before ‘>’ token"中的两个模板

这个问题在这里已经有了答案:WhereandwhydoIhavetoputthe"template"and"typename"keywords?(8个答案)关闭7年前。最小工作示例:#includestructPrinter{templatestaticvoidprint(Telem){std::coutstructMain{templatevoidprint(Telem){//Inthiscase,thecompilercouldguessTfromthecontext//Butinmycase,assumethatIneedtospecifyT.printer_t::print(e

c++ - 获取模板函数类型

我是在C++中使用模板的新手,我想根据之间使用的类型做不同的事情。和>,所以function()和function()不会做同样的事情。我怎样才能做到这一点?templateT*function(){if(/*Tisint*/){//...}if(/*Tischar*/){//...}return0;} 最佳答案 您想使用函数模板的显式特化:templateT*function(){};templateint*function(){//yourint*functioncodehere};templatechar*function()