草庐IT

TEMPLATE

全部标签

c++ - 使用可变参数模板的特化作为模板参数

考虑以下几点:templatestructMyT;templatestructMyT{};templateclassTT=MyT>structA{};//fineusingB=A;//doesnotcompileintmain(){return0;}当MyT用作A的默认参数时,编译器(g++5.4.0)很满意。但是,当它用于实例化A时,情况就不同了:temp.cpp:19:16:error:type/valuemismatchatargument1intemplateparameterlistfor‘templateclassTT>structA’usingB=A;^temp.cpp:

c++ - 模板默认类型与默认值

此问题遵循previousone.案例一:默认类型以下程序无法编译并报告errorC2995:'Tfoo(void)':functiontemplatehasalreadybeendefined:#include#includetemplate::value>>Tfoo(){std::cout::value>>Tfoo(){std::cout();foo();}每个模板都由两个foo实例交替使用和忽略(SFINAE)。所以我假设编译器在某个时候看到:templateTfoo(){std::coutTfoo(){std::cout两个定义是一样的,错误有点可以理解。也许不太容易理解的是为

c++ - 如何与 "template using"定义的模板(别名)类成为 friend ?

类B想和每个人成为friendC.我正在努力寻找解决方法。只要我不添加有问题的行,下面是成功编译的完整代码。#includeusingnamespacestd;enumEN{EN1,EN2};templateclassC{public:C(){std::coutclassB{templateusingCT=C;//templatefriendclassCT;//ct;}};intmain(){B::test();return0;}这是我尝试过的(全部失败):-templatefriendclassC;templatefriendclassCT;templatefriendclassCT

c++ - 使用 constexpr 函数时推导失败?

所以我正在根据thisquestion做进一步的测试,我仍然不太清楚类型推导的工作原理。如果我使用以下内容:template::value,int>=0>inlineautofnx(T)->int{return0;}template::value,int>=0>inlineautofnx(T)->int{return0;}inlinevoidfn(){fnx(1);fnx(1.f);}我没有得到任何编译错误。但是当我这样做时:templateconstexprboolvalue(){returnTRUTH;}template::value>(),int>=0>inlineautofnx

c++ - 通过 CRTP 检测模板类继承的元函数

我有一个这样的界面:templateclassInterface{...}及其具体实现:templateclassConcrete:publicInterface,T>{...usingtype=typenameT;}我想要一个元函数来检查某个类型是否来自Interface。举个例子,假设接口(interface)只有一个模板参数(因此它不会生成子模板类):templateclassA{...}classB:publicA{...}在这种情况下,我可以使用:templatestructis_A{staticboolconstvalue=std::is_base,T>::value;}我

c++ - C++ 模板的部分特化 : template parameter not deducible

下面的代码工作正常:templateclassFib{};templateclassFib{};但是下面的代码显示错误为:Error:templateparametersnotdeducibleinpartialspecialization:templateclassFib{};templateclassFib{};你能解释一下这种行为的原因吗? 最佳答案 我相信您只是缺少部分特化的正确语法:templateclassFib{};templateclassFib{};模板上的第一个参数是类型,而第二个只是一个常量值。

c++ - template<class = enable_if_t<...>> 做什么?

我一直在阅读STL文件,以学习格式化代码的更好方法,并学习提高效率的技巧。我一直在阅读线程文件,但我无法弄清楚某些代码的作用。template,thread>::value>>explicitthread(_Fn&&_Fx,_Args&&..._Ax){//constructwith_Fx(_Ax...)...}std::enable_if_t是templateusingenable_if_t=typenameenable_if::type;templatestructenable_if{//typeis_Tyfor_Testusingtype=_Ty;};该代码在thread和str

c++ - 实例化模板参数的参数包

我想制作一个模板,它接受一组模板并使用相同的参数包实例化它们。不幸的是,我似乎无法弄清楚如何在模板参数包中扩展参数包。如何编译?#include#includetemplatetypename...Args>structTupleTupleMaker{templateusingNewTupleTuple=typenamestd::tuple...>;};templateusingtuple1=std::tuple;templateusingtuple2=std::tuple;usingexpected=std::tuple,std::tuple>;usingactual=TupleTu

c++ - 在模板类中定义的枚举上嵌套类的部分特化

请看下面的代码:templatestructX{enumclassE{e0};templatestructY{};templatestructY{staticintf(){return0;}};};intmain(){X::Y::E::e0>::f();}VC++15.7.5生成错误消息:1>test.cpp1>some_directory\test.cpp(15):errorC2039:'f':isnotamemberof'X::Y::E::e0>'1>some_directory\test.cpp(15):note:seedeclarationof'X::Y::E::e0>'1>s

c++ - std::bind 可变参数模板和自动返回类型

遵循thisquestion中的代码,我有一个带有可变参数模板函数的std::bind。如果我尝试提供带有auto返回值的函数模​​板,gcc会拒绝该程序:#includetemplateautoinv(Args...args){autobound=std::bind(&inv_impl,args...);returnbound;}intmain(){autob=inv(1,2);}编译错误为:foo.cc:Ininstantiationof‘autoinv(Args...)[withArgs={int,int}]’:foo.cc:41:30:requiredfromherefoo.c