草庐IT

variadic-templates

全部标签

c++ - 在类型说明符中使用 "simple-template-id"

在C++11标准中,dcl.type.simple和dcl.type.elab部分声明类型说明符可以包括simple-template-编号。另一方面,根据temp.names部分,simple-template-id可以表示函数模板特化。真的可以使用函数模板特化来指定类型吗? 最佳答案 7.1.6.2/2Theothersimple-type-specifiersspecifyeitherapreviously-declareduser-definedtypeoroneofthefundamentaltypes(3.9.1).强调

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++ - 如何在运行时恢复函数指针的类型

在代码中,我在管理器类中注册了一个或多个函数指针。在这个类中,我有一个映射,将函数的参数类型映射到所述函数。它可能看起来像这样:std::map,void*>templatevoidRegister(Ret(*function)(Args...)){void*v=(void*)function;//recursivelybuildtypevectorandaddtothemap}在运行时,代码使用任意数量的参数获取调用(来自外部脚本)。这些参数可以作为原始数据类型或将在编译时指定的自定义类型读取。对于脚本的每次调用,我都必须找出要调用的函数,然后调用它。前者很容易并且已经解决了(在循环

c++ - 如何存储可变类型的参数?

这个问题在这里已经有了答案:variadictemplateclasstomakeadeferredcalltoavariadictemplatefunction(2个答案)关闭8年前。我想从action成员函数而不是构造函数中调用这个foo函数。为此,我必须将值存储在某处。我无法弄清楚执行此操作的语法。#includevoidfoo(inta,intb){std::coutstructFoo{public:Foo(Args...args){foo(args...);}voidaction(){}private://Args...?};intmain(){Foox(1,2);}

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++ - 关联 std::tuple 容器

是否有可能定义(以一种简单的方式,可能重新使用std容器)“associativestd::tuple”,或者换句话说“variadiacstd::map”。类似这样的东西(这个接口(interface)只是为了解释,欢迎其他可能的接口(interface)):AssociativeTupleat;//std:stringisthekeytypeat.insert("my_float",3.14);//1.at.insert("my_int",42);at.insert("my_bool",true);at.insert("xyz",0);at.insert("my_string","

c++ - 折叠任意多个可变参数包

我正在阅读EricNiebler的post在他的小型元编程库上。在尝试实现他遗漏/列为挑战的部分时,我只剩下以下transform的实现:templateusingmeta_apply=typenameF::templateapply;templatestructtypelist_transform;//unarytemplatestructtypelist_transform,F>{usingtype=typelist...>;};//binarytemplatestructtypelist_transform,typelist,F>{usingtype=typelist...>;}

c++ - 使用带有参数包扩展和附加值的静态存储持续时间初始化 std::array

在询问时anotherquestion最近,在用参数包扩展后跟另一个元素初始化std::array时,我偶然发现了GCC的一些奇怪行为。我已经与Jarod42简要讨论过这个问题inthecommentsthere但我认为最好将其作为一个新问题提出。例如,考虑下面的代码,它应该提供一个实用程序make_array函数,该函数接受任意数量的参数并将它们std::forward发送到std::array初始化。前导标记参数选择数组是否应以默认构造的T(通过std::true_type选择)或不(通过std::选择)终止false_type).然后我创建一个整数数组,一次使用静态,一次使用自动

c++ - 当参数是函数参数包时,在部分排序期间推导模板参数

N452714.8.2.4[temp.deduct.partial]3Thetypesusedtodeterminetheorderingdependonthecontextinwhichthepartialorderingisdone:(3.1)—Inthecontextofafunctioncall,thetypesusedarethosefunctionparametertypesforwhichthefunctioncallhasarguments.(3.2)—Inthecontextofacalltoaconversionfunction,thereturntypesofth