草庐IT

template-specialization

全部标签

C++ 多参数模板化类成员特化

我正在设置一个c++(11)程序,我在其中使用了一个依赖于2个参数的模板化类。该类的大部分内容都可以根据模板参数进行通用编写。只有少数功能需要专门的版本。这是重现我的问题的示例模式:templateclassfoo{//typedefsandmembersthatdependonTandN//butthatcanbewrittengenericallye.g.:typedefstd::arraymyarray;voidmyfunc(myarraytab);};//...templatefoo::myfunc(myarraytab){//genericversion}//needspec

c++ - 错误 : 'Failed to specialize function template' C2893 'std::invoke'

我正在VisualStudio2013中编写MFC程序,但我不断收到以下两个错误错误C2893无法特化函数模板'unknown-typestd::invoke(_Callable&&,_Types&&...)'和错误C2672“std::invoke”:找不到匹配的重载函数错误与文件xthread第238行有关我是c++/MFC的新手,我正在尝试编写一个将在后台运行到系统时间的函数。这是我使用的代码:voidtask1(ExperimentTab&dlg){while(true){CStringshowtime=CTime::GetCurrentTime().Format("%H:%M

c++ - 使用模板元编程计算数据编译时间

假设我们有这样的代码。它运行良好,可以预先计算前5个斐波纳契数。#includetemplatestructfib;templatestructfib{constexprstaticintvalue=1;};templatestructfib{constexprstaticintvalue=1;};templatestructfib{constexprstaticintvalue=fib::value+fib::value;};intmain(){std::cout::value::value::value::value::value::value但是它有一个“小”问题。如果我们需要将

c++ - 为不同的特征专门化相同的运算符

我想通过特征特化来做以下事情。ArrayAa=Scalarin_a将使用重载I。ArrayAa=ArrayBb将使用overloadII。在下面的代码中,永远不会使用overloadII。有人提到T1不能在overloadII中推导。如何解决?我使用C++shell用C++14编译代码。#include#includeusingnamespacestd;classA;//forwarddeclaration.templatestructis_A:false_type{};templatestructis_A:true_type{};templatestructis_int:false_

c++ - 内部类的模板特化

考虑以下代码:structX{templateclassY{};};templateclassX::Y{};这里我们专门针对double类型的Y类,代码运行良好。问题是,如果我将代码更改为:templatestructX{templateclassY{};};templateclassX::Y{};编译器会报错:'X::Y':explicitspecializationisusingpartialspecializationsyntax,usetemplateinstead!有人知道在这种情况下我如何专攻Y类吗? 最佳答案 如果不显

c++ - 检查模板函数是否存在

我怎样才能像这样检查模板函数的存在:检查reader结构是否有read算术值structreader{templatestd::enable_if_t::value,T>read(){return{};}};我使用这样的检查器:templatestructtest_read{staticconstexprautovalue=std::is_convertible().read()),int>::value;};但是编译器提示:error:wrongnumberoftemplatearguments(1,shouldbe2)staticconstexprautovalue=std::is

c++ - va_list 还在 C++ 中使用吗?还是鼓励使用 template<typename...T>?

在C中,定义可变长度参数的唯一方法是使用省略号声明其原型(prototype)并使用va_list、va_start、va_arg,va_end来提取它们。就像printf系列和scanf系列一样。在C++11中,引入了如下新方法。templatevoidfunc(Targ,MoreT...args){//Dosomestufffunc(args);}每种方法的优点和缺点是什么?在C++中是不鼓励使用还是鼓励使用它们中的任何一个? 最佳答案 在C++中强烈不鼓励使用C风格的可变参数函数。风格各不相同,但编写这些类型的函数会让您在某些

c++ - Curiously Recurring Template Pattern (CRTP) 是正确的解决方案吗?

场景考虑一个Logger类,它有一个为标准C++类型重载的成员函数write(),还有一些方便的函数模板,比如writeLine()内部调用write():classLogger{public:voidwrite(intx){...}voidwrite(doublex){...}...templatevoidwriteLine(Tx){write(x);...}...};进一步考虑一个子类FooLogger,它为特定于域的类型添加了额外的write()重载(我们称其中两个为FooType1和FooType2):classFooLogger:publicLogger{public:usi

c++ - 错误 : use of deleted function for overloaded template

我正在尝试模板特化,但无法确定为什么charconst*const无法在下面解析(尽管是有效类型)的原因。templateBfoo(A)=delete;templatevoidfoo(char*){}templatevoidfoo(charconst*const){}intmain(){{//typesOKcharconst*consta=nullptr;char*b=nullptr;}char*data;foo(data);//OKfoo(data);//ERRORreturn0;}错误error:useofdeletedfunction‘Bfoo(A)[withA=constcha

c++ - "run time templates"

我很确定答案是“你不能使用模板,你必须使用虚函数(动态多态性)”,但如果我走那条路,我似乎必须复制很多代码.这是设置:我目前有两个类,ColorImageSegmentation和GrayscaleImageSegmentation。他们做的事情本质上是一样的,但是有3个区别-它们对不同类型(ColorImage和GrayscaleImage)进行操作-一个参数,直方图的维度(3vs1)不同-PixelDifference函数根据图像类型不同如果我创建一个类templateclassImageSegmentation{};我会保持良好的状态。但是,我想让这个对象成为另一个类的成员:cl