草庐IT

TEMPLATE

全部标签

c++ - C++如何考虑常量性,模板化性和泛型性来解析专用模板?

我有以下代码,可能看起来有些费解,但来自真实代码:#includeusingnamespacestd;templatevoidfoo(Hrm&h,A&a){coutclassHrg>voidfoo(Hrg&h,int&a){coutclassHrg>voidfoo(Hrg&h,constint&a){coutstructwhat;templatestructwhat{};templatestructwhat{};intmain(){whatwt;whatwf;inti=5;constint&ri=i;foo(wt,i);//1)genericfoo(wf,i);//2)speciali

c++ - 了解 C++ 模板元编程

为了更好地理解C++中的模板和元编程,我正在阅读thisarticle,但我对代码片段的理解很快就减少了,例如:templateclassB>structmp_rename_impl;templateclassA,class...T,templateclassB>structmp_rename_impl,B>{usingtype=B;};templateclassB>usingmp_rename=typenamemp_rename_impl::type;代码使用如下:mp_rename,std::tuple>//->std::tuplemp_rename,std::pair>//->s

c++ - 可变参数模板和 C 数组

我正在尝试编译以下代码:templatevoidfoo(constT(&array)[N]){}templatestaticintargs_fwd_(Tconst&t){foo(t);return0;}templatevoidmycall(Us...args){intxs[]={args_fwd_(args)...};}intmain(void){intb[4];mycall(b);}mycall函数使用可变参数模板,然后转发到args_fwd_函数以在每个参数上调用函数foo。这对大多数参数类型都适用(假设我已经适本地定义了foo函数)。但是当我尝试传递一个C风格的数组(intb[4

c++ - 从 std::true_type 继承 vs static constexpr const bool 成员

我知道这不是一个非常尖锐的问题。使用一个比另一个有优势(编译时间、依赖性、调试符号大小、可用性、可读性等)吗?templatestructIsSharedPtr:std::false_type{};对比templatestructIsSharedPtr{staticconstexprboolvalue=false;};相关问题...templatestructS;templatestructS{};templatestructS{};对比templatestructS;templatestructS{};templatestructS{}; 最佳答案

c++ - 外部模板 'inconsistent explicit instantiations'

给定#include//CaseI:errorerrorC2961:'std::vector>':inconsistentexplicitinstantiations,apreviousexplicitinstantiationdidnotspecify'externtemplate'templateclassstd::vector;externtemplateclassstd::vector;//CaseII:fine//externtemplateclassstd::vector;//templateclassstd::vector;//CaseIII:fine//externte

c++ - 模板什么时候结束?

模板什么时候结束?让我们看一下这段代码:templateclassthatClass{Ta,b;thatClass(Tx,Ty){a=x;b=y;}};templateTaFunc(Tone,Ttwo){returnone+two;}那么template什么时候开始结尾?它总是在类或函数定义的末尾之后结束还是什么?为什么不能只使用为类和函数声明的一个模板,所以在这种情况下,我可以使用模板参数T对于函数aFunc以及类定义? 最佳答案 模板参数的范围以模板化主题的范围结束:templateclassthatClass{Ta,b;tha

c++ - 在 C++ 模板实例化期间获取原始结构/类名

templatestructS{};templatestructR{};intmain(){typedefSs1;typedefSs2;typedefRs3;static_assert(xxx::value,"No,assertionmustnotberaised");static_assert(xxx::value,"Yes,assertionmustberaised");}所以,我想要xxx::value在xxx::value时返回true在编译时返回false。xxx在C++中是不可能存在的吗?或者,在C++中理论上可能存在xxx但可能还没有人这样做过?

c++ - 如何在允许空数组的情况下使用模板函数计算 C++ 数组项

我使用以下模板函数来计算数组项:#includetemplateconstexprsize_tcountof(T(&)[N]){returnN;}intmain(void){struct{}arrayN[]={{},{},{}};printf("%zu\n",countof(arrayN));return0;}它有效,但不适用于空数组:struct{}array0[]={};printf("%zu\n",countof(array0));gcc5.4输出:error:nomatchingfunctionforcallto‘countof(main()::[0])’note:candid

c++ - 我应该如何进行这种显式特化?

下面的设计是否可行?templateclassTest{public:templatevoiddoSomething();//restofthingsprivate:Tobj;//somethings};现在,如果可能的话,我会为doSomething做一些明确的特化,这样最后我会得到如下所示的一些版本:voiddoSomething(){//dosomething}voiddoSomething(){//dosomething}...etc这似乎是不可能的,我找不到任何语法来完成这项工作,然后我想也许设计应该如下所示,以便所有模板参数都应该传递给模板类本身:templateclass

c++ - enable_if'ed 模板化模板构造函数的类型签名?

我通常先声明我的类和模板,然后再定义它们的方法(当然是在同一个头文件中)。我只是觉得这样更容易阅读。好吧,我遇到过这样一种情况,我无法找出在类外定义中使用的有效类型签名。这是我正在做的一个简化示例,它说明了问题:templatestructFoo{Foo(Ta,Tb);template>>Foo(Iteratorfirst,Iteratorlast);};templateFoo::Foo(Ta,Tb){...}templatetemplateFoo::Foo(Uf,Ul){...}我在WHAT_GOES_HERE槽中尝试了很多方法来尝试获得匹配的签名,但我一直失败。我需要enable_