草庐IT

Third_Parameter

全部标签

c++ - 默认参数模板与可变参数模板 : what is the last template parameter?

我有点困惑,因为默认参数模板和可变参数模板参数都必须是模板的最后一个参数。那么我的函数的良好官方语法是什么?templatemyFunction(/*SOMETHING*/)或templatemyFunction(/*SOMETHING*/) 最佳答案 实际上,模板参数包和默认参数没有是函数中的最后一个,如果它之后的任何内容将被推断(或默认):templatevoidf(T3){}请注意,您永远不能为T2指定任何内容,因为所有内容都将被可变参数包吞噬。由此得出结论,如果要手动指定可变参数包,则将可变参数包放在默认参数之后是有意义的。

c++ - "local variables at the outermost scope of the function may not use the same name as any parameter"是什么意思?

我一直在阅读C++入门第5版。在第6.1章功能参数列表的第三段中。它写道“此外,函数最外层范围内的局部变量不得使用与任何参数相同的名称”。什么意思?我不是以英语为母语的人。我不明白函数的“最外层范围”的实际含义。 最佳答案 函数的最外层是定义函数体的block。您可以将其他(内部)block放入其中,并在该block的本地变量中声明变量。内部block中的变量可以与外部block中的变量或函数参数具有相同的名称;他们将名称隐藏在外部范围内。外部block中的变量不能与函数参数同名。演示:voidf(inta)//functionha

C++ 元编程 : A template parameter which *must* inherit an abstract class

我有一个用于可比较+哈希值的抽象类:classKey{public:virtualbooloperator==(constKey&)const=0;virtualbooloperator!=(constKey&)const=0;virtualu32hashcode()const=0;};还有一些继承这个的具体类C。classC:publicKey{private:u32a,b;public:staticconstC&null;//aprototypeforrepresentinga"novalue"C//Somereasonableimplementation;it'sjustapai

C++11 `using` 关键字 : specialize template alias of template parameter

我今天在使用using时遇到了问题C++11中的关键字.我决定现在使用另一种方法(在下面的示例中添加为注释)。你可以想到X作为矩阵,Y作为mixin,目的是访问X的转置矩阵类型在Y.而不是typedef学习X在X,我们采用另一种更强大的方法并定义Sibling本身带有两个模板参数的别名。templatestructX{usingLeft=A;usingRight=B;templateusingSibling=X;//usingReversed=X;//WhatIreallywantandusenow.:-)};templatestructY{usingLeft=typenameA::L

C++ OO设计: Inheritance of template parameter

我有一个以Base为基类的继承链。我希望能够编写一个继承Base和可能的另一个Base派生类的类模板。我可以使用虚拟继承,但我找到了另一种解决方案。我想知道它是否是常见的/可观的/合法的类设计:编写一个类模板,其中模板参数是它派生的类,即它必须是Base或Base派生类。在构造函数中,我可以使用静态断言来真正确保用户没有使用任何非法类作为模板参数。如果它有效,我将永远不会有虚拟继承问题......问题是,这样做是可以的。我在其他项目中从未见过它,所以我想在使用它之前先确定一下。编辑:为了确保我不会混淆你,这里有一些代码:classBase{};classDerived:publicBa

c++ - 可变参数模板错误 : "parameter pack must be expanded"

这是我编写的可变参数模板函数:templateValue&insert(Container&c,Args&&...args){c.emplace_back(args);returnc.back();}当我像这样使用insert时出现错误:listlst;int&num=insert,int,int>(lst,4);错误提示insert正文中的这一行:c.emplace_back(args);//这是什么意思,我该如何解决? 最佳答案 错误是由于在将所有单个参数(而不是参数包)传递给emplace_backargs之后缺少省略号(..

c++ - "trailing parameter pack"到底是什么

在解决函数模板重载之间的歧义时,会执行部分排序(参见here的一些解释)。在那个网站上,我们还了解到Incaseofatie,ifonefunctiontemplatehasatrailingparameterpackandtheotherdoesnot,theonewiththeomittedparameterisconsideredtobemorespecializedthantheonewiththeemptyparameterpack.现在,我想知道尾随参数包到底是什么。如果有的话templatestructtuple{/*...*/};templatevoidfoo(tupl

c++ - 使用强 typedef 作为 Boost Parameter 库的更轻量级替代方案?

我经常使用Booststrongtypedef实用程序来boost我的程序的安全性。例如通过编写如下代码:BOOST_STRONG_TYPEDEF(int,X)BOOST_STRONG_TYPEDEF(int,Y)BOOST_STRONG_TYPEDEF(int,Width)BOOST_STRONG_TYPEDEF(int,Height)structRect{Rect(Xx,Yy,Widthw,Heighth);};//Usage:Rectrect(X(10),Y(20),Width(800),Height(600));这里的强typedefboost了代码的可读性和安全性。(如果参数

c++ - 构造函数 : difference between defaulting and delegating a parameter

今天,我偶然发现了thesestandarddeclarationsstd::vector构造函数://untilC++14explicitvector(constAllocator&alloc=Allocator());//sinceC++14vector():vector(Allocator()){}explicitvector(constAllocator&alloc);这种变化可以在大多数标准容器中看到。一个稍微不同的例子是std::set://untilC++14explicitset(constCompare&comp=Compare(),constAllocator&al

c++ - 奇怪的编译器错误 : Cannot convert parameter from 'int' to 'int &&'

这到底是怎么回事?我正在尝试创建一对int和一个string如果我使用“魔术值”但似乎无法传递变量,我可以创建这对.std::vector>num_text;std::stringtext="Smeg";intnum=42;//Worksfinenum_text.push_back(std::make_pair(42,std::string("Smeg")));//Cannotconvertparameter2from'std::string'to'std::string&&'num_text.push_back(std::make_pair(42,text));//Cannotcon