草庐IT

template-specialization

全部标签

c++ - 如何根据类的模板参数专门化模板成员函数?

我想这样写:templateclassOK{T1t1;T2t2;public:templateconstTX&GetRef()const;};templatetemplateconstT1&OK::GetRef()const{returnt1;}templatetemplateconstT2&OK::GetRef()const{returnt2;}哪个VS10编译失败。为了检查我对模板特化的理解,我尝试并编译了这个:typedefintT1;typedefcharT2;classOK{T1t1;T2t2;public:templateconstTX&GetRef()const;};te

c++ - 成员函数的模板特化

我必须在(C++,我使用的是MSVisualStudio2008SP1)中对类成员函数使用显式特化,但我无法成功编译它。获取errorC2910:'File::operatorclassFile{std::ofstreammOutPutFile;public:templateFile&operatorFile&File::operatorFile&File::operator(std::ofstream&out){mOutPutFile 最佳答案 您对operatorTconst&data与std::ofstream&out。这个在

c++ - 函数签名的专用模板

在该测试代码中:#include#includeusingnamespacestd;templateclassSignal;templateclassSignal{public:Signal(T(*ptr)(U)){}};voidPrint(stringconst&str){coutsig=&Print;return0;}为什么我要写templateclassSignal;?为什么我必须指定它? 最佳答案 您不必做您正在做的事情,但这是最灵活的方法。具有特化的单参数模板如下所示:在一种类型上参数化的模板...templatestru

c++ - 实现Matlab的冒号: operator in C++ expression templates class

我正在实现一个C++表达式模板库。我已经设置了一个适当的SubMatrixExpr类来收集矩阵中的元素,启用类似的语法B=SubMatrix(A,1,3,2,10);相当于Matlab的B=A(1:3,2:10);当然,Matlab的语法比我的要舒服得多。所以我的问题是有没有可能在C++中设置Matlab的冒号:运算符?非常感谢您。 最佳答案 简短的回答:没有。冒号不是有效的C++运算符,因此不能重载。即使可以,它仍然不可能轻松实现您的需求,因为它肯定会优先于逗号运算符,这将使您的表达式位于A((1:3),(2:10))行中。,如果

c++ - Cryptic template 模板参数错误

我正在尝试创建一个从std::map或std::unordered_map获取键的函数。我可以使用简单的重载,但首先我想知道这段代码有什么问题。templateclassTContainer>std::vectorgetKeys(constTContainer&mMap){std::vectorresult;for(constauto&itr(std::begin(mMap));itr!=std::end(mMap);++itr)result.push_back(itr->first);returnresult;}当使用std::unordered_map调用它时,甚至手动指定所有模板

c++ - 专门为私有(private)类(class)的功能?

有没有办法为私有(private)类专门化一个函数(比如,std::swap)?例如,当我测试这个时:#includeclassOuter{structInner{inta;voidswap(Inner&other){usingstd::swap;swap(this->a,other.a);}};public:staticvoidtest();};namespacestd{templatevoidswap(Outer::Inner&a,Outer::Inner&b){a.swap(b);}}voidOuter::test(){usingstd::swap;Innera,b;swap(a

C++:如何在模板类中部分特化模板函数

代码说话:templatestructVector3D{Groupx,y,z;Vector3D(Groupx,Groupy,Groupz):x(x),y(y),z(z){}templateGroupNorm()const;};templatetemplateGroupVector3D::Norm()const{returnpow(pow(x,p)+pow(y,p)+pow(z,p),(1.0/p));}/*templatetemplateGroupVector3D::Norm()const{returnsqrt(x*x+y*y+z*z);}*/注释block在vc11(vs2012)中

c++ - 模板特化不起作用

考虑以下代码:templateclassBar{};templateclassFoo{};templatetemplatestructFoo,Bar>{structEq{};};如您所见,有一个可变类型Bar和一个类型Foo.Foo有一个专门化,以防两个Bars用作其模板参数。这个特化有一个内部类型Eq.但是,以下内容不起作用:typenameFoo,Bar>::Eqb;它告诉我们没有类型Eq在Foo,Bar>,即编译器不选择模板特化而是选择Foo的基本定义确实没有内部Eq类型。我在这里做错了什么?为什么编译器不选择特化? 最佳答案

c++ - 嵌套类声明 : template vs non-template outer class

我有一个C++模板类,里面有一个嵌套类,比如:templateclassOuter_t{public:classInner;Inneri;};templateclassOuter_t::Inner{public:floatx;};intmain(){Outer_to_t;//3oranyarbitraryinto_t.i.x=1.0;return0;}编译没有任何问题。然而,一旦我声明了一个类似的非模板类,就像这样:classOuter_1{public:classInner;Inneri;};classOuter_1::Inner{public:floatx;};intmain(){

c++ - 如何正确地显式实例化具有完全特化成员的模板类?

假设我们有以下文件:foo.hnamespacens{templateclassFoo{public:Foo();~Foo();voidDoIt();};}foo.cpp#include"foo.h"#includenamespacens{templateFoo::Foo(){std::coutFoo::~Foo(){std::coutvoidFoo::DoIt(){std::coutvoidFoo::DoIt(){std::cout;templateclassFoo;}假设该类型只会与int或double作为类型参数一起使用,这是进行显式实例化的正确方法吗?或者您是否也需要在头文件中