草庐IT

2Template

全部标签

c++ - 错误 : ‘list’ is not a member of ‘std’ and error: template argument 2 is invalid

我正在尝试编译我的头文件,但我遇到了我无法弄清楚的错误。我想创建一个包含3个映射的结构:-从单个单词映射到计数-从词对映射到计数-从单个单词映射到后续单词列表我的头文件中的代码:#include#include#include#include#include#include#include#includetypedefstruct{std::mapfirstCounts;std::mappairCounts;std::map>follows;//Youcanuseaniteratortoretrievethevaluesstoredinthelist.}LanguageModel;我得

c++ - 模板特化是否需要 template<> 语法?

我有一个类似这样的访客类:structVisitor{templatevoidoperator()(Tt){...}voidoperator()(boolb){...}};很明显,operator()(boolb)旨在成为上述模板函数的特化。但是,它没有template语法,我以前经常看到它,将其声明为模板特化。但它确实可以编译。这样安全吗?这是正确的吗? 最佳答案 您的代码不是模板特化,而是非模板函数。那里有一些差异。非模板化operator()将优先于模板化版本(对于精确匹配,但类型转换不会在那里发生)但您仍然可以强制调用模板化

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