草庐IT

TEMPLATE

全部标签

c++ - 模板类中的模板可变参数函数无法编译

我正在尝试为模板类编写一个函数,该函数接受一个参数,该参数是大类私有(private)数据中成员类的函数指针。当您调用该成员时,它会在较小的类上调用该函数。(混淆吧?)为了演示,我在这里有一个不起作用的例子:#include#includeusingnamespacestd;templatestructMyClass{templateautocall_me(Ffunc,A...args){//passinthefunctionwewanttocallreturn(mContainer.*func)(args...);//callthefunctionsuppliedby//thepar

c++ - 使用可变参数模板构建函数参数

我有这个示例代码,它可以满足我对3参数函数的需求:templateTGETPARAM(void){returnT();}templateintGETPARAM(void){return123;}templatedoubleGETPARAM(void){return1.2345;}templateconstchar*GETPARAM(void){return"hello";}templateRESBuildArgs3(RES(*fn)(P1,P2,P3)){P1p1=GETPARAM();P2p2=GETPARAM();P3p3=GETPARAM();returnfn(p1,p2,p3)

c++ - 关于太多模板标题的警告

我收到与previousdiscussion中相同的警告消息但我不明白如何解决:warning:toomanytemplateheadersforfoo::value(shouldbe0)intconstfoo::value=1;当有人想使用以下玩具头时会出现警告消息:#ifndefFOO#defineFOOtemplatestructfoo;templatestructfoo{staticintconstvalue;};templateintconstfoo::value=1;#endif你能解释一下这里的问题是什么吗? 最佳答案

c++ - 使用模板化方法将类作为 GCC 的模板参数传递

这个问题在这里已经有了答案:C++templatecompilationerror:expectedprimary-expressionbefore‘>’token[duplicate](1个回答)WhereandwhydoIhavetoputthe"template"and"typename"keywords?(8个答案)关闭4年前。是否可以在GCC下将具有模板化方法的类的对象作为模板参数传递?我进行的所有搜索都指向了有关模板化类的问题,这不适用于我的情况。示例代码:#includeclassFoo{public:templatevoidprint(constT&arg)const{

c++ - 模板模板参数的显式匹配

考虑这个函数:templateclassC,classT,classAlloc>voidfoo(C&container){std::cout函数foo()接受std::vector,但它也接受std::map在C++17中因为Compare和Allocator有默认值。备注std::map在C++14中失败。我怎样才能制作foo()只接受只有2个模板参数的模板,所以失败并显示std::map在C++17中? 最佳答案 HowcanImakefooonlyaccepttemplateswithexactlyonly2templatep

c++ - 静态多维数组,编译时维度定义

是否有任何语法可以在编译时定义静态数组的维数?假设我有一个大小为D的元组,其值为d_0,...,d_{D-1}。我希望能够创建一个数组Tarr[d_0][d_1]...[d_{D-1}]。有没有办法在编译时实现这一点?我特别询问静态数组语法,而不是如何嵌套结构。这里有一个代码片段来阐明我想要实现的目标:templatetypenameContainer,autoDimC,auto...Dim>structNestedContainer{usingtype=Container::type,DimC>;};templatetypenameContainer,autoDim>structNe

c++ - 模板参数阴影在 VS2005 中如何工作?

在GCC中,这段代码无法编译,因为T被隐藏了,但是在VS2005中,它编译时没有警告,那么VS编译器所做的假设是什么?templateclassFoo{templatevoidBar(constT&bar){...}}; 最佳答案 经过3个月的搜索找到了正确的答案:)它在标准的14.6.1/4中:Atemplate-parametershallnotberedeclaredwithinitsscope(includingnestedscopes).Atemplate-parametershallnothavethesamenamea

c++ - 想要 : a C++ template idea to catch an issue, 但在编译时?

我们有一个常量结构数组,像这样:staticconstSettingsSuT_table[]={{5,1},{1,2},{1,1},etc};结构如下:size_bytes:num_items:其他“元数据”成员所以“总大小”是单个元素的size_bytes*num_items。所有这些信息都在const数组中,在编译时可用。但是,请注意,_table的总大小与EEPROM本身的大小无关。_table不镜像EEPROM,它只描述了布局、用途和我们需要的其他“元数据”类型的信息。但是,您可以使用此元数据来确定我们正在使用的EEPROM的数量。数组简单地描述了存储在外部EEPROM中的数据

c++ - 从基类指向成员的指针类型

我对成员指针有疑问。以下代码无法使用OracleSolarisStudio12.2的CC和cygwinGCC4.3.4进行编译,但可以使用MicrosoftVisualC++2010:structA{intx;};structB:publicA{};templateclassBar{public:templatevoidfoo(MT::*p);};intmain(int,char*[]){Barbbar;bbar.foo(&B::x);return0;}在倒数第二行,上述两个编译器都未能找到Bar::foo(intA::*)的匹配项.我写了一个简单的测试来确认表达式的类型&B::x实际

c++ - ...使用时没有模板参数错误

我再次需要你的帮助...我有以下代码,它导致了在非命名空间范围内的显式特化错误:namespace__test{templateclassTest{templatevoidcheck(C&a){}templatevoidcheck(C&a){}//error:explicitspecializationinnon-namespacescope'class__test::Test'};}因为我已经知道如何修复此类错误,所以我在类范围之外定义了特化,但是我遇到了另一个错误-...usedwithouttemplateparameters:namespace__test{templatevo