草庐IT

TEMPLATE

全部标签

c++ - 如何在 C++ 中用枚举专门化函数的返回类型?

我正在使用一个变体来为C++语法分析器存储一系列类型。语法规则的每个组成部分都有一个类别(枚举类型)和一个值。成分根据类别存储一种类型的值。为了举例,我将类别简化为“String”=>存储一个字符串,“Number”=>存储一个int。我想根据其类别枚举获取具有正确类型的成分的值。我该怎么做?我在下面编写了示例代码,其中我构造了两个组成部分:strCon,存储一个字符串,以及intCon,存储一个int,并尝试获取它们的值。我想把strCon中的字符串赋值给strVal,以及从intCon到intVal的int。#includestructConstituent{enumclassCa

c++ - Friend 模板函数(在非模板类中),C++

如果我有一个非模板(即“普通”)类并希望有一个模板友元函数,我该如何编写它而不导致编译器错误?这是一个示例来说明我正在尝试做的事情:templatevoidbar(T*ptr);classMyClass//notethatthisisn'tatemplateclass{private:voidfoo();templatefriendvoidbar(T*);//ERROR:compilergivesmeallkindsofgrief};templatevoidbar(T*ptr){if(ptr){MyClassobj;obj.foo();}}我使用的是VisualStudio2005,我

c++ - 如何编写接受每个类和类模板的 C++ 模板?

预先警告:这个问题似乎比实际情况更明显。我想编写一个可以接受任何具体类或模板类作为模板参数的模板。这可能看起来毫无用处,因为如果不知道传入的T是否已模板化,您将不知道如何使用它。我想要这个的原因是我可以声明一个没有定义的通用模板,然后用户可以专门化。因为用户是特化的,所以他们总是知道他们正在处理的类型。但是,如果不先声明模板,用户就无法特化模板。你可以这样做:templateclassmyclass;但是如果你传入一个模板化的T,那将不起作用,例如myclass不会工作。那么我们试试这个:templateclassmyclass;templateT>classmyclass;这可能是正

c++ - 省略 C++ 模板参数列表时的区别

什么时候可以省略C++模板参数列表?例如,在VisualStudio2010中,这段代码编译得很好:templateVec2Vec2::operator+(constVec2&v)const{returnVec2(x+v.x,y+v.y);}如果你内联代码,它实际上编译时没有任何参数列表。但这真的和下面的版本一样吗?templateVec2Vec2::operator+(constVec2&v)const{returnVec2(x+v.x,y+v.y);} 最佳答案 在类中你可以省略类类型的参数:templatestructA{Af

c++ - 显式成员特化

g++3.4.5接受此代码:templatestructA{staticconstchar*conststr;};structB{};typedefAC;templateconstchar*constC::str="B";//Equivalenttofollowing?//templateconstchar*constA::str="B";但我不确定它是否真的合法C++03。特别是,[14.7p3]Inanexplicitspecializationdeclarationforaclasstemplate,amemberofaclasstemplateoraclassmembertem

c++ - 函数模板不能隐藏类名?

这适用于GCC和Comeau:structX{};voidX(){}这在Comeau中中断了:structX{};templatevoidX(){}这打破了两者:templatestructX{};templatevoidX(){}该规则由§3.3.7/2定义。差异仅仅是因为函数模板不是函数吗?我无法理解GCC的行为。Aclassname(9.1)orenumerationname(7.2)canbehiddenbythenameofavariable,datamember,function,orenumeratordeclaredinthesamescope.Ifaclassoren

c++ - "Name The Template Parameter"奇定义

template//whyisboolhere,classbooltype=bool.Aretheyequivalent?structif_{typedeftypenamettype;};templatestructif_//whatdoesthemean?{typedeftypenameutype;};代码来自一篇名为“命名模板参数”的文章。我无法理解both结构的定义。 最佳答案 编辑:首先移动最重要的部分:if_::type;//thisisintif_::type;//thisisdouble这很方便地定义了一个可以在编译时

c++ - 没有模板重新绑定(bind)的 typedef 模板。作为模板类参数的模板使用

我想做这样的事情:templateclassBaseSubscriber{};templateclassBasePublisher{//notworking:invaliduseoftemplate-name'BaseSubscriber'withoutanargumentlisttypedefBaseSubscriberSubscriberType;//compilingtypedefBaseSubscriberSubscriberTypeT;};templateclassSubscriber,classData>classClassA:publicSubscriber{};temp

c++ - Clang 访问修饰符顺序和 decltype

我一直在考虑创建一个同步器助手模板类,它基于HerbSutter在这个talk中的包装器类的想法。这在msvc中不起作用(除非我们删除大括号初始化)但是当大括号初始化被删除时就没问题了。在clang/gcc(ubuntu12.10,gcc4.7.2,clang(3.2)selfbuiltwithlibc++)中,private访问修饰符似乎必须出现在public之前:这看起来有点奇怪。gcc的错误是错误:“t_”未在此范围内声明clang是error:useofundeclaredidentifier't_'autooperator()(Ff)const->decltype(f(t_)

c++ - 内联导致覆盖虚函数的模板类的专门成员函数被忽略

我想和你们分享一个我偶然发现的奇怪的例子,这让我思考了两天。要让这个例子正常工作,您需要:三角形虚继承(成员函数getAsString())模板类的成员函数特化(此处为Value::getAsString())覆盖虚函数(自动)由编译器内联你从一个模板类开始,它实际上继承了一个公共(public)接口(interface)——即一组虚函数。稍后,我们将特化其中一个虚函数。内联可能会导致我们的特化被忽视。//test1.cppandtest2.cpp#includeclassValueInterface_common{public:virtual~ValueInterface_commo