草庐IT

typedeffing

全部标签

c++ - 如何理解这个声明中的typedef

最近看了EffectiveC++这本书,第35条里面有一个关于typedef的声明让我很困惑。classGameCharacter;//Question1:Whyuseforwarddeclaration?intdefaultHealthCalc(constGameCharacter&gc);classGameCharacter{public:typedefint(*HealthCalcFunc)(constGameCharacter&);//Question2:Whatdoesthismean?explicitGameCharacter(HealthCalcFunchcf=defau

c++ - 在 C/C++ 中使用 `typedef` 的暗角?

我最近发现了DarkcornersofC/C++:Thetypedefkeyworddoesn’tneedtobethefirstwordontheline并且惊讶地发现下面所有看起来很奇怪typedef在C和C++中都是有效的:inttypedefa;shortunsignedtypedefb;上面的typedef等同于:typedefinta;typedefshortunsignedb;并且,以下内容在C++中无效但在C中有效:typedefenum{c};typedef;typedefint;typedefintshort;语言设计者在C和C++中留下这个黑暗角落的原因是什么?

c++ - typedef 和枚举或枚举类

我有一个这样的枚举:(实际上,它是一个枚举类)enumclasstruth_enum{my_true=1,my_false=0};我希望能够将my_true暴露给全局命名空间,这样我就可以这样做:chara_flag=my_true;或者至少:chara_flag=(char)my_true;取而代之的是:chara_flag=truth_enum::my_true;这可能吗?我试过这样的:typedeftruth_enum::my_true_true_;我收到错误:枚举类truth_enum中的my_true没有命名类型我的猜测是my_true是一个值而不是类型。有没有其他方法可以让

c++ - 检查类是否具有 typedef(私有(private)或其他)的特征

有没有办法检查class有一个typedef这甚至适用于privatetypedef?以下代码在VS2013中有效,但在ideone'sgcc上失败templatestructto_void{typedefvoidtype;};classFoo{typedefintTD;};templatestructhas_TD:std::false_type{};templatestructhas_TD::type>:std::true_type{};intmain(){std::cout::value编辑-我为什么要这个我有自定义序列化系统,可以序列化任意类型。当它必须表现不同时(例如字符串),

c++ - typedef 和显式实例化之间的代码重复

树.htemplateclassbinary_operation:publicnode{//...unimportantdetails...unsignedevaluate()const;voidprint(std::ostream&os)const;};typedefbinary_operation,'+'>addition;typedefbinary_operation,'*'>multiplication;//...树.cpptemplateunsignedbinary_operation::evaluate()const{//...unimportantdetails...}t

C++检查模板参数的嵌套typedef以获得其标量基类型

考虑下面的指数平滑器模板类。此类用于以指数方式平滑/过滤顺序数据(请参阅更新方法)。Elemtype可能是一个vector,而Floattype通常是一个标量。例如ExponentialSmootherx(0.1,Vector2f(0.5,0.5));在这个例子中,第二个模板参数Floattype可以避免,因为Eigen的Matrix类包含一个嵌套的typedef来获取标量基类型:Vector2f::Scalar将Elemtype和Floatype都实例化为float来平滑一维数据也是合理的。在这种情况下,也可以跳过第二个模板参数。templateclassExponentialSmo

c++ - 使用 typedeffing 模板化基类来简化代码是一种好习惯吗?

最近在处理许多模板化类并从它们派生时,我发现自己“发明”了这个简单的结构。我不确定这是常见做法,还是我在脖子上系了一根绳子。templateclassBase{};templateclassDerived:publicBase{typedefBaseBase;};我发现如果Base它特别有用类有自己的typedefs对于某些类型。例如:templateclassBase{typedefTScalar;typedefMatrixMatrix;};然后很容易将类型“导入”到Derived中.它节省了重新键入模板签名。例如:templateclassDerived:publicBase{ty

c++ - typedef type * type::* ,它是什么?

我有以下代码:structmyType{myType*ptr;};typedefmyType*myType::*other_type;第二行typedef'ining是什么?这是一个返回myType指针或其他东西的成员函数吗? 最佳答案 将other_type定义为指向myType成员的指针,其中所述成员本身是指向myType的指针。例如,您可以这样使用它:other_typex=&myType::ptr;myTypemine;mine.*x=&mine;为什么你会这样做,我不能说。 关

c++ - typedef a std::string - 最佳实践

我正在用标准C++编写一个库来进行语音转换。到目前为止,我已经使用了std::string。但将来我可能不得不将其更改为其他内容(std::wstring或其他内容)。所以我需要以一种可以轻松切换的方式编写我的库。到目前为止,我已经完成了以下工作。创建了一个将被所有CPP文件使用的头文件为此添加了“typedefstd::string”,并在文件中各处使用了新名称。如果我需要改变类型,我可以简单地在头文件中改变,它会在所有地方反射(reflect)出来。如果有人能看出这是正确的方法或者有更好的方法来做到这一点,我将不胜感激?谢谢 最佳答案

c++ - 检测并报告 typedef 错误……这是在做什么?

我正在阅读有关通过使用固定宽度整数使代码更具可移植性的内容。我找到了this帮助解释事情的文章,最后它建议使用这个匿名union来检测和报告typedef错误:staticunion{charint8_t_incorrect[sizeof(int8_t)==1];charuint8_t_incorrect[sizeof(uint8_t)==1];charint16_t_incorrect[sizeof(int16_t)==2];charuint16_t_incorrect[sizeof(uint16_t)==2];charint32_t_incorrect[sizeof(int32_t