草庐IT

2Template

全部标签

c++ - Clang (OS X) 在特定的嵌套声明中需要 "template"关键字,而 VS 禁止它

我正在使用两个编译器(Xcodev5.0.2上的Clang和VisualStudio2012Update4)编写一个跨平台应用程序,我遇到了两个编译器不同意使用所需语法的场景嵌套声明中的template关键字。这是代码(归结为一个易于重现的测试用例):templatestructBase{templatestructInnerBase{};};templatestructDerived:publicBase{//the"template"keywordisREQUIREDinClang/OSXstructInnerDerived:publicBase::templateInnerBas

c++ - "template"关键字之前的 "class"关键字在做什么?

只是一些代码示例[不是现实生活中的例子]//atfilescopetemplatestructdemo{};templateclassdemo;//isthetemplatekeywordoptionalhere?第3行中的模板关键字是可选的吗?我以前没有(经常)看到模板关键字的这种用法。标准的哪一部分说允许这样做?编辑我认为g++有一个错误。templatestructdemo{};classdemo;//templatekeywordomitted在g++(4.5.1)上编译而在Comeau上失败"ComeauTest.c",line5:error:specializingclas

c++ - 没有参数的模板类 `template<>` 是什么意思?

没有参数的模板类是什么意思?例如,让我们以计算阶乘的模板类为例,其模板参数为N-N!。.基本上,这是类:templateclassFactorial{public:enum{fact=N*Factorial::fact};};但是,我发现这个类有一个“扩展类”,templateclassFactorial{public:enum{fact=1};};我的问题是:没有参数的模板是什么,template什么意思?提前致谢。 最佳答案 这个templateclassFactorial{public:enum{fact=1};};实际上是类

c++ - 提升 : dereference a template argument if it's a pointer

如果它是一个指针(或智能指针),我可以使用什么来取消引用模板参数,或者如果它不是,我可以保持原样吗?templatevoidsubf(constT&item){item.foo();}templatevoidf(constT&item){subf(magic_dereference_function(item));}Boost中的任何内容都是一个选项。 最佳答案 templateT&maybe_deref(T&x){returnx;}templateT&maybe_deref(T*x){return*x;}您必须单独为智能指针添加重

c++ - "use of class template requires template argument list"是什么意思?

我是模板的新手,所以请原谅我的幼稚问题。我在这段代码中遇到错误:templateclassa{public:inti;a(t&ii):i(ii){}};intmain(){a*a1(newa(3));cout编译错误:'a':使用类模板需要模板参数列表'a':类没有构造函数 最佳答案 使用a*a1(newa(3));^^^^^^^^^如果你想让你的模板参数被自动推导,你可以使用一个辅助函数:templatea*createA(constT&arg)//pleaseaddconsttoyourctor,too.{returnnewa(

c++ - 为什么我们必须做 template <class/typename> T 而不仅仅是 template T

代替templatevoidfunc(Targ){/*something*/}为什么我们做不到templatevoidfunc(Targ){/*something*/}来自cplusplus.com:Theonlydifferencebetweenbothprototypesistheuseofeitherthekeywordclassorthekeywordtypename.Itsuseisindistinct,sincebothexpressionshaveexactlythesamemeaningandbehaveexactlythesameway.对我来说,这似乎是不必要的样板

c++ - 我可以在这里使用 Curiously Recurring Template Pattern (C++) 吗?

我有一个C++应用程序,可以简化为如下所示:classAbstractWidget{public:virtual~AbstractWidget(){}virtualvoidfoo(){}virtualvoidbar(){}//(othervirtualmethods)};classWidgetCollection{private:vectorwidgets;public:voidaddWidget(AbstractWidget*widget){widgets.push_back(widget);}voidfooAll(){for(unsignedinti=0;ifoo();}}void

c++ - 默认模板参数 : Why does the compiler complain about not specifying template argument?

我有这个代码:structA{};templatestructB{voidfoo(){}};Bb;//Error:missingtemplateargumentsbefore'b'//Error:expected';'before'b'//Moreerrorsb.foo()如果我将foo()作为具有相同模板“签名”的模板函数,编译器不会提示没有指定模板参数:structA{};structB{templatevoidfoo(){}};Bb;//OKb.foo()那么为什么我需要为带有默认参数的模板类指定参数,而不是为模板函数指定参数呢?我是否遗漏了一些微妙之处?原因肯定是因为模板参数推

c++ - "Invalid template argument"错误在 Visual Studio 但不是 GCC

假设你有代码templateclassBaseType>classEST16:publicBaseType{public:EST16(doubled){}};templateclassSCEST{Ty;};typedefEST16EST16_SC;classChild:publicEST16_SC{public:Child():EST16_SC(1.0){}};classNotWorkingChild:publicEST16{public:NotWorkingChild():EST16(1.0){}};TEST(TemplateTest,TestInstantiate){Childch

c++ - `class template Example<int>;` 语句对 C++11 意味着什么?

有人提到我"ExplicitTemplateInstantiation"在cplusplus.com,它给出了以下示例:templateclassExample{public:Example(Ttest){_data=test;}voidsetTest(Ttest){_data=T;}private:T_data;};classtemplateExample;classtemplateExample;classtemplateExample;除了在我看来是一个遗漏错误之外,试图将类型分配给成员变量--_data=T而不是我认为应该是_data=test--我不明白的是最后3行究竟声明或