草庐IT

template-specialization

全部标签

c++ - 了解如何在编译时计算总和

我有这段代码,想了解一下:templatestructsum;templatestructsum{enum{value=size};};templatestructsum{enum{value=size+sum::value};};int_tmain(intargc,_TCHAR*argv[]){sum::value;return0;}我不明白为什么必须存在未实现的总和(采用无符号...就像最后一个结构特化,没有冲突吗?)以及如何使用模板部分的相同参数来特化总和(e.g.sum与template相同。为什么波纹管不起作用?templatestructsum{enum{value=siz

c++ - 两阶段查找 : is it possible to easily mix inheritence and templates

简介:C++标准区分依赖模板参数的符号名称和不依赖模板参数的名称,这称为两阶段名称查找(参见here)。定义模板时,会尽快解析非相关名称。另一方面,从属名称仅在模板实例化时解析。示例:templatestructBase{typedefTtype;staticconstintn=3;virtualintf()=0;intf(intx){returnx*2;}};//doesn'tcompile!templatestructDerived:Base{typefield;//Thecompilerdoesn'tknowBase::typeyet!intf(){returnn;}//thec

c++ - 避免模​​板特化中的构造函数重复

假设我有一个基类存储对某些classBar的引用:classFooBase{public:FooBase(Bar&ctx):_barCtx(ctx){};virtual~FooBase(){};//Someotherfunctionsprotected:Bar&_barCtx;};我想做的是在此之上添加一个继承级别,其中classFoo会增加一些功能。templateclassFoo:publicFooBase{public:Foo(Bar&ctx):FooBase(ctx){};booldoSomething(inta);};然后,有一些实例Foo需要提供不同版本的doSomethi

c++ - 嵌套模板类中的 std::conditional

我正在尝试以STL的样式实现RingBuffer。这意味着我还为其实现了一个迭代器,它必须作为const或非const工作。这只是迭代器部分:#include#includetemplateclassRingBuffer{public:classIterator;//actualRingBufferimplementationhere};templateclassRingBuffer::Iterator{public:typedefstd::ptrdiff_tdifference_type;typedefTvalue_type;typedeftypenamestd::condition

c++ - SFINAE 优雅地检查 "template template class"(在模板参数中)

如何在模板参数中检查模板模板类的类型?例子B和C是模板类。我想创建一个类D那可以是D或D.只有D有D::f().这是我的解决方法(demo)。它有效。#includeusingnamespacestd;classDummy{};templateclassB{};templateclassC{};templateclassBC>classD{//f()isinstantiatedonlyif"BC"=="B"public:template>statictypenamestd::enable_if>::value,void>::typef(){}//^#1};intmain(){D::f(

c++ - 警告 C4661 :no suitable definition provided for explicit template instantiation request

我写了一个类模板并在不同的DLL中使用它,所以希望隐藏部分实现。为此,我使用“模板实例化”,但导出它,像这样,这里是头文件:#include#includeusingnamespacestd;templateclass__declspec(dllexport)Templated{public:Templated();};template__declspec(dllexport)Templated;intmain(){cout并且定义在单独的文件(.cpp)中templateTemplated::Templated(){}templateTemplated;我的问题是我收到警告,即使实例

C++ 模板特化 - 避免重新定义

我想要一个接受不同类型参数的通用函数(或方法)。如果提供的类型有“一个”方法,函数应该使用它。如果它有“两个”方法,则该函数应该使用它。这是无效代码:#includetemplatevoidfunc(Typet){t.one();}templatevoidfunc(Typet)//redefinition!{t.two();}classOne{voidone(void)const{std::cout是否可以使用SFINAE来实现?是否可以使用type_traits来实现?澄清:如果使用SFINAE可以做到这一点,我会更高兴。最好的情况是:使用第一个模板,如果失败则使用第二个。检查方法是

c++ - 专门化模板类的模板成员函数?

我有一个模板类,它有一个需要专门化的模板成员函数,如:templateclassX{public:templatevoidY(){}templatevoidY(){}};虽然VC正确处理了这个问题,但显然这不是标准的,GCC提示:explicitspecializationinnon-namespacescope'classX'我试过:templateclassX{public:templatevoidY(){}};template//Alsotried`template`herevoidX::Y(){}但这导致VC和GCC都提示。正确的做法是什么? 最佳答

c++ - 静态字段初始化的模板部分特化

我正在尝试以下操作:structMyType{};templatestructTest{staticconstMyType*constsm_object;};templatestructTest{staticconstMyType*constsm_object;};templateconstMyType*constTest::sm_object=newMyType();templateconstMyType*constTest::sm_object=newMyType();我将其包含在2个文件中-a.cpp和b.cpp。我尝试编译并得到:errorC2998:'constMyType*

c++ - 成员访问和模板特化

我有这个类模板templateclassWrapper{public:virtualvoidparse(std::strings)=0;protected:Tvalue;};理想情况下,每种类型都应该知道如何从字符串中解析自身,所以我想有,例如,专门化templateclassWrapper{public:virtualvoidparse(std::strings){value=atoi(s.c_str());}};但是,显然,我无法从主模板访问“值”成员。我得到的是这样的:Inmemberfunction'virtualvoidWrapper::parse(std::string)'