草庐IT

Archive_Template

全部标签

C++ : Vector of template class

我有一个名为Cell的模板类,如下所示:-templateclassCell{stringheader,Tdata;}现在我想要另一个名为Row的类。Row将有一个名为Cells的vector,这样我就可以将Cell和Cell类型的元素添加到该vector中。可能吗?如果是这样,我该怎么做?提前致谢。 最佳答案 根据您提供的额外详细信息,前两个答案将无效。您需要的是一种称为单元格变体的类型,然后您可以获得这些类型的vector。例如:-enumCellType{Int,Float,//etc};classCell{CellTypet

c++ - variadic template template中的Variadic template推导

我不确定这个标题是否有意义,但这个例子实际上很简单://Aconverterstructwithagenericconstructor.templateclassTT>structconverter{templateconverter(constTT&);};//Afewclasstemplates.templatestructfoo{};templatestructfoo2{};templatestructfoo_variadic{};templatestructfoo_variadic2{};intmain(){//Allthesecompile.converter(foo{});

c++ - "class template"与 "template class"

这个问题在这里已经有了答案:关闭12年前。PossibleDuplicate:Whatisthedifferencebetweenatemplateclassandaclasstemplate?我见过几个C++大师因为调用类似的东西而抨击人们templateclassSomeClass{//...};模板类而不是类模板。(请注意,这不是什么大错特错,但表明某人不是经验丰富的C++程序员)是的,正确的词是“类模板”——因为它是用于生成类的模板。但我不明白为什么在典型的对话中区分很重要。没有人听/读你写的东西会明白你所说的是什么意思。在标准或其他方面是否有特定使用“模板类”一词的情况,这使

c++ - 铿锵错误 : non-type template argument refers to function that does not have linkage -- bug?

我有一些非常简单的(C++11)代码,最新的clang(version3.4trunk187493)无法编译,但GCC编译正常。代码(下面)实例化函数模板foo使用局部函数类型Bar然后尝试将其地址用作类模板Func的非类型模板参数:templatestructFunc{};templateexterninlinevoidfoo(){usingFoo=Func>;}intmain(){structBar{};//function-localtypefoo();return0;}clang发出以下错误:error:non-typetemplateargumentreferstofunct

c++ - Const temporary from template type 以及为什么使用 std::add_const?

以下代码摘自cppreference.com.#include#includestructfoo{voidm(){std::coutvoidcall_m(){T().m();}intmain(){call_m();call_m::type>();}但是,当使用VC++Nov2012CTP编译时,输出为Non-cvNon-cv而不是预期的:Non-cvConst另外,下面两个语句有什么区别:call_m();和call_m::type>(); 最佳答案 这似乎是MSVC的一个错误。使用T()形式的表达式(就标准而言,这是一种显式类型转

c++ - g++ 编译器错误 : couldn't deduce template parameter ‘_Funct’

我正在尝试使用ANSIC++for_each语句迭代并打印标准vector的元素。如果我让for_each调用一个非重载函数,它会工作,但如果我让它调用一个重载函数,则会产生编译器错误。这是一个最小的测试程序,用于显示编译器错误发生的位置:#include#include#includestructS{charc;inti;};std::vectorv;voidprint_struct(intidx);voidprint_struct(conststructS&s);//f:anon-overloadedversionoftheprecedingfunction.voidf(const

c++ - 为什么 C++1* 仍然需要 template 关键字来代替 Full Duck Typing

很多年前,(至少对我而言)静态C++多态性似乎是连贯的。Python等语言依赖ducktyping,你有:deffn(foo,bar):foo.baz(bar.bo())当时的想法是,如果它适本地“嘎嘎叫”,那么语言就没问题。相反,在C++中,您必须解释它是什么“动物”:voidfn(foo_typefoo,bar_typebar);对于“家庭王国”,您明确需要使用template关键字:templatevoidfn(Foofoo,Barbar);具有像auto...()->decltype这样的新功能返回类型,尤其是genericlambdas,似乎有一些更像是非模板Python类的

c++ - 是否有可能有一个像 'template<class T> X(){}' 这样的模板化构造函数?

structX{templateX(){}};是否可以实例化这种类型? 最佳答案 是的,有这样一个构造函数是可能的,但是调用它是不可能的。模板化构造函数的所有模板参数都必须从参数列表中推导出来或具有默认值。在您的示例中,您无法实例化该类。[临时内存][Note:Becausetheexplicittemplateargumentlistfollowsthefunctiontemplatename,andbecauseconversionmemberfunctiontemplatesandconstructormemberfuncti

c++ - 模板参数不明确 : could not deduce template argument

我正在做一些看起来像这样的包装器:#includetemplatevoidApply(void(T::*cb)(Value),T*obj,Valuev){(obj->*cb)(v);}classFoo{public:voidMyFunc(constint&i){std::cout我收到这个错误:应用:未找到匹配的重载函数。voidApply(void(__thiscallT::*)(Value),T*,Value):模板参数Value不明确,可能是int或constint&。voidApply(void(__thiscallT::*)(Value),T*,Value):无法从const

c++ - 从 boost::archive::text_oarchive_impl 和 boost::archive::text_iarchive_impl 派生自定义存档类

注意:Boost的存档方案基于对称的输入和输出存档类。一直写这两者很乏味,所以我将使用?archive来表示oarchive和iarchive。总结:将自定义存档的基类从binary_?archive_impl更改为text_?archive_impl后,当编译器实例化时,我的自定义存档类不再“找到”>serialize(...)我的其他类中的方法。背景:我的应用程序使用binary_?archive_impl的子类成功地读取和写入文件到磁盘(文档和/或代码注释建议这优于从binary_?archive派生)。我需要从二进制文件格式切换到文本格式,因此我将自定义存档的基类切换为text