草庐IT

TEMPLATE

全部标签

c++ - 专门化非模板类的可变参数模板成员函数

这段代码有问题:#includeusingnamespacestd;classA{public:templatevoidstuff(Args...args);};templatevoidA::stuff(Args...args){coutvoidA::stuff(){cout();b.stuff();}Tryingtocompileit,我得到这个错误:template-id'stuff'for'voidA::stuff()'doesnotmatchanytemplatedeclaration我做错了什么?我在没有可变参数的情况下尝试过它并且它有效,但是我如何专门化可变参数模板成员函数

c++ - 为什么我的函数不跳过尝试解析为不兼容的模板函数,而是默认解析为常规函数?

这个问题在这里已经有了答案:Whycan'tatemplatefunctionresolveapointertoaderivedclasstobeapointertoabaseclass(1个回答)关闭8年前。std::stringnonSpecStr="nonspecializedfunc";std::stringconstnonTemplateStr="nontemplatefunc";classBase{};classDerived:publicBase{};templatestd::stringfunc(T*i_obj){(*i_obj)+=1;returnnonSpecStr

C++ - 模板特化和部分特化

我一直在Internet和stackoverflow上寻找具体答案,但似乎找不到。我必须创建一个通用类,然后实现特定功能。我的具体指示是:您需要使用模板表达式参数和模板类特化和部分特化。我有一个模板类:templateclassZ{T**array[x][y];public:Z();voidprint();//andothermethods};我需要:1)只有x=2和y=2的Z需要有一个公共(public)方法voidJ()2)对于x=2和y=2的字符Z,J会做一些事情;对于其他一切,它会做其他事情3)只有当T是char时,Z才会将数组初始化为某个值。其他一切都是0当然,这是可行的:t

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

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

c++ - 如何授予函数模板好友访问类的权限?

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Howtoallowtemplatefunctiontohavefriend(-like)access?如何让函数模板Loadfriend访问类Foo?这里的目标是限制对构造函数的访问:只有函数模板Load可以构造。CODE(请忽略内存泄漏)classFoo{Foo(){}templatefriendFooconst&Load();//errorhere};templateTconst&Load(){return*(newT);}intmain(intargc,char*argv[]){Fooconst&f=

c++ - 来自可变参数模板的固定数量的模板参数

templateclassF>structcall_me{};templatestructmaybe;templatestructmore;intmain(){call_mea;//okcall_meb;//error}我明白为什么call_me失败。但我想让它发挥作用。有没有不涉及更改call_me的解决方法?(或为其添加特化)? 最佳答案 templateclassF>structcall_me{};templatestructmaybe;templatestructmore;templateclassF>structjust_

c++ - 未使用的模板方法中的错误

structB{inta;voidfoo(){a=5;}};templatestructA{A(inti){B::foo();}A(doubled){}};intmain(){Aa(5.0);}gcc4.7.2编译它没有错误。clang3.4svn提示:$clang-Wall-Wextratest.cpptest.cpp:10:16:error:calltonon-staticmemberfunctionwithoutanobjectargumentA(inti){B::foo();}~~~^~~代码当然是错误的,但是哪个编译器是符合标准的呢?同样奇怪的是,如果您使用5而不是5.0,c

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++ - 任意但编译时已知数量的类型的元组

假设我有一个由另一个完整的POD类型参数化的类型:templatestructMyFoo{/*...*/};有了它,就有可能拥有它们的元组:typedefstd::tuple,MyFoo,MyFoo>Foo3;但是现在,我想要一个类型“Foo”,其中N是constexpr.一种实现类似于Foo的方法会是:templatestructFoos;templatestructFoos{typedefstd::tuple>type;};templatestructFoos{typedefstd::tuple,MyFoo>type;};/*continuewiththis....*/即为我想要的

c++ - 类模板中的静态成员变量

当您有一个包含静态成员的(非模板化)类时,例如:classFoo{public:staticintx;};然后Foo::x必须在一个且只有一个翻译单元中定义,否则编译器会提示多个定义。所以在somefile.cpp你必须定义它:intFoo::x=10;这样,任何访问Foo::x的翻译单元正在访问相同的内存地址。但是如果Foo怎么办?是类模板吗?templateclassFoo{public:staticintx;};现在,Foo::x可以在头文件中定义:templateintFoo::x=10;所以,如果类模板Foo在foo.hpp中定义,和translation_unit1.cpp