草庐IT

template-specialization

全部标签

c++ - 为什么bitset要用template来实现?

要定义一个16位的位集,就像这样:std::bitsetbs(0x123);如果让我设计一个bitset类,我大概会这样:mine::bitsetbs(16,0x123);std::bitset是由模板实现的有什么原因吗?这是我们应该在某些情况下应用的好模式吗? 最佳答案 Isthereanyreasonthatstd::bitsetisimplementedbytemplate?Isthisagoodpatternthatweshouldapplyinsomesituations?因为std::bitset被设计成一个静态位集。在

c++ - 错误 : Use of class template requires template argument list

当我尝试运行我的程序时,此错误显示为“errorC2955:'FOURTEEN':useofclasstemplaterequirestemplateargumentlist”#includeusingnamespacestd;templateclassFOURTEEN{private:Ta[n];public:voidReadData();voidDisplayData();};voidFOURTEEN::ReadData(){for(inti=0;i>a.[i];}voidFOURTEEN::DisplayData(){for(inti=0;i>a.[i]P;//Readdatai

c++ - 我可以将 `extern template` 放入头文件中吗?

在头文件中放一个外部模板,然后在单元编译文件中显式模板实例化有效吗?例如在g++的编译示例中,这是为了避免nothing的实例化吗?两次?为什么没有人这样写而更喜欢复制externtemplate每个.cpp文件中的行?A.hpp:#ifndefHEADERC_A#defineHEADERC_Atemplatestructnothing{};externtemplatestructnothing;#endifA.cpp:#include"A.hpp"templatestructnothing;ma​​in.cpp:#include"A.hpp"#includeintmain(){not

c++ - 如何通过未定义类型定义元函数?

请考虑像这样的元函数#includetemplatestructSum:std::integral_constant{};templatestructProduct:std::integral_constant{};他们的结果可以通过::value提取出来成员:static_assert(Sum::value==7,"3+4==7");static_assert(Product::value==10,"2*5==10");两个元函数都有相似的静态签名。也就是说,他们关联了一个T每对T在哪里T受到与std::integral_constant相同的限制。并且要么是可加的,要么是可乘的。所

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++ - 如何与 "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++ - 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++ 模板 - 浮点型和整型的不同特化

我正在尝试编写一个带有如下签名的函数:templateTobar(Fromin){...}这个函数需要有不同的行为取决于ifTo是float型还是积分型。(假设From是整数且都是算术)这可以使用ifconstexpr(std::is_integral::value){...}else{...}轻松实现,但是我仅限于没有ifconstexpr的C++11.实现这种特化的好方法是什么? 最佳答案 您可以将模板重载与SFINAE一起使用.例如templatetypenamestd::enable_if::value,To>::typeb

c++ - 为什么我的函数模板特化被 VS2017 拒绝而不是被 VS2015 拒绝?

我有一个将类型与整数值相关联的特征类。structtraits{private:templatestructtype_impl{};templatestructtype_impl{usingtype=int;};//...public:templateusingtype=typenametype_impl::type;};我正在编写一个模板函数,其返回类型由上面的traits类提供,并将其专门用于各种int值:templatetraits::typefunction();templateinlinetraits::typefunction(){return42;};//...这在VS2