草庐IT

TEMPLATE

全部标签

c++ - 使用不带参数的模板名称

我有这个代码:templateclasstemplatedclass{public:usingtype=templatedclass;};templateclasssinkstuff{public:voidprint(){coutclasssinkstuff>{public:voidprint(){coutstructpass_parameter:sinkstuff::type>{};intmain(){pass_parameterobj;obj.print();cout,typenametemplatedclass::type>::value;//1,yes}我一直认为“using指

c++ - Variadic 模板类,从其参数列表中获取特定类型的索引

是否可以实现一个可变参数模板类的函数成员,该函数成员返回可变参数列表中给定类型的索引。我看到的问题是创建某种伪造的可变参数列表,只是为了触发编译时模板评估。templateclassFoo{templateint_get_idx(inti,constTArg&curr,TArgs...args){if(std::is_same(T,TArg)){returni;}else{returnget_id(i+1,args...);}}用法类似于:Foofoo;inti=foo.get_idx();//i==1 最佳答案 你可以使用类似的东

c++ - 有没有比 allocator_type 更好的方法来区分可调整大小的容器?

我有operator>>()的模板重载,我需要区分可以调整大小的容器(例如vector)和不能调整大小的容器(例如,数组。我目前只是在使用allocator_type特征(见下面的代码)——它工作得很好——但想知道是否有更明确的测试方法。templatestructis_resizable{typedefuint8_tyes;typedefuint16_tno;templatestaticyestest(classU::allocator_type*);templatestaticnotest(...);staticconstboolvalue=sizeoftest(0)==sizeo

c++ - 从模板函数调用静态模板方法

我正在尝试执行调用另一个类的模板静态方法的模板方法,但我遇到了一些编译错误。最小的情况如下。如果我编译下面的代码templateintfoo(){returnD::bar()+1;}它抛出以下输出g++-std=c++11test.cpp-ctest.cpp:Infunction‘intfoo()’:test.cpp:4:18:error:expectedprimary-expressionbefore‘>’tokenreturnD::bar()+1;^test.cpp:4:20:error:expectedprimary-expressionbefore‘)’tokenreturnD

c++ - 如何 static_assert 给定的函数调用表达式是否可以编译?

有没有一种方法可以检查函数调用表达式是否会在编译时进行编译并对其进行static_assert?还是我应该通过system()调用编译器并检查退出代码?#includetemplatevoidf(Args&&...args,bool);templatevoidg(Args&&...args);intmain(){g(1,2.0,"hello",false);//compilesf(1,2.0,"hello",false);//doesn'tcompile//HowdoIdothis?//static_assert(!does_this_compile(f(1,2.0,"hello",f

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

重构遗留代码我想合并彼此相关的单独模板类/结构(以避免命名空间污染)。Nested(下)是MyStruct的辅助类,我想将其移入MyStruct。但我无法完成这项工作:#include#includestructYES{};structNO{};templatestructMyStruct{templatestructNested{staticvoidPrint(void){std::cout::Print()"structNested::value,YES>::type>{staticvoidPrint(void){std::cout::Print()"编译器提示:g++-O0-g3

c++ - 来自 cppreference.com 的模板参数特化示例不起作用

我在http://en.cppreference.com/w/cpp/language/partial_specialization上找到了这个例子templatestructB{};templatestructB{};//OK:firstparameterisdeducible我在使用-std=c++11和-std=c++14编译时出错如何编译这个?或者也许例子是错误的?error:templateargument‘(I*2)’involvestemplateparameter(s)templatestructB{};//OK:firstparameterisdeducible

c++ - enable_if 检查迭代器的值类型是否是一对

我想为值类型为一对的迭代器编写一个专门的模板函数。我的期望是这应该匹配std::map的迭代器。为了检测对:templatestructis_pair:std::false_type{};templatestructis_pair>:std::true_type{};//alsotriedthis,butitdidn'thelptemplatestructis_pair>:std::true_type{};然后我在函数声明中使用enable_if:templatedecltype(auto)do_stuff(std::enable_if::value,ITR>itr){//access

c++ - 如何使用 C++ 模板执行正式协议(protocol)?

当使用templatestyle固有的编译时鸭子类型时,有什么方法可以强制要求模板参数实现具有特定签名的特定方法?structProtocolT{voidg()const;voidh();}//IwantthecompilertocheckthatTconformstoProtocolT//thatis,Tmustimplementg()andh()ratherthanjustg()templatevoidf(constT&x){x.g();}当然,即使没有这个,也有完美的类型安全:如果模板参数T没有在模板函数实现中使用的方法,编译器总是会报错。但我发现明确声明classT必须具有某些

c++ - template<> 用于成员枚举的显式特化

根据17.7.3[temp.expl.spec]第5段(N4659),...Membersofanexplicitlyspecializedclasstemplatearedefinedinthesamemannerasmembersofnormalclasses,andnotusingthetemplatesyntax.Thesameistruewhendefiningamemberofanexplicitlyspecializedmemberclass.However,templateisusedindefiningamemberofanexplicitlyspecializedm