草庐IT

pseudo-typedef

全部标签

c++ - Typedef 具有特定构造函数参数的类

让我们从一个简单的C++类开始:classaClass{boolb;aClass(boolx){b=x;}};是否可以对2个新类型stateTrue和stateFalse进行typedef,这样如果我这样做:stateTruevariable;它会转化为:aClassvariable(true);? 最佳答案 继承的替代方法是将aClass设为template:templateclassaClass{public:boolb;aClass():b(T){}};typedefaClassstateTrue;typedefaClasss

c++ - 如何访问可变模板参数包成员中存在的内部模板 typedef?

我有一些代码对我来说似乎没有歧义,但gcc4.7令人窒息:#include#includeusingnamespacestd;//Containerformixinstemplateclass...Mixins>structMix:Mixins>...{typedeftuple>...>types;};//OuterlayerextractsthetypetuplefromtheargumenttemplatestructInnerCombiner{typedeftypenameInnerCombiner::typetype;};//Typedeftypetobeanewmixofth

c++ - pointer_traits 为既不是 X<A, T...> 也没有提供成员 typedef element_type 的类型提供什么?

下面的结果是什么?它是格式错误、未定义的行为还是定义良好且格式正确的行为?structA{};std::pointer_traitsx;我之所以问,是因为好奇想知道,也想知道任意类型是不是指针。我还想包括shared_ptr和friend。我想知道是否有类型特征(谓词),如果没有,我是否可以使用pointer_traits并检测是否声明了element_type。 最佳答案 它表示从20.6.3p1开始格式错误,因为它没有element_type并且不是类模板实例化typedefseebelowelement_type;Type:P

C++ 使用别名访问嵌套类型(使用 vs typedef)

今天我们发现了一个令人困惑的C++11别名声明行为。这是示例:templatestructQ{typedefTt;};templatevoidfoo(Qq){usingq_t=Q;//typedefQq_t;//ifweuncommentthisandcomment'using'theexamplecompilestypenameq_t::tqwe;//’}intmain(intargc,char*argv[]){Qq;foo(q);return0;}ISO14882(C++11)规定这两个声明必须具有相同的语义(第145页)。但是,如果我们用“using”声明了q_t,则该示例不会使

c++ - 带有类模板 typedef 参数的函数模板

以下代码是我在大型项目中尝试做的事情的示例:#include#include//standardtemplatetypedefworkaroundtemplatestructmyvar{typedefstd::vectorType;};templateTmax(typenamemyvar::Type&x)//Tmax(std::vector&x){Ty;y=*x.begin();for(typenamemyvar::Type::iteratorit=x.begin();it!=x.end();++it)if(*it>y)y=*it;returny;}intmain(intargc,ch

c++ - typedef 到模板类型

下面的问题是什么?typedefboost::shared_ptrSharedPtr;GCC给出以下错误:ISOC++forbidsdeclarationof‘shared_ptr’withnotype 最佳答案 C++(还)没有“模板类型定义”,您可以在其中“重命名”这样的模板。这是C++0x中添加的一个特性,其中这样的“typedef”被称为“别名模板”。目前最简单的解决方法是使用带有嵌套typedef的类模板:templatestructSharedPtr{typedefstd::shared_ptrType;};//usag

c++ - typedef-name 作为基类 : illegal but widely tolerated

[class.derived]的第一段说的是基类说明符,Ifthenamefoundisnotaclass-name,theprogramisill-formed.但是,一个simple测试表明Comeau和g++-ansi-pedantic都接受typedef-name作为基础。Boostheader上的简单grep-r'[^:]:mpl'表明流行的库通常依赖于这种行为。是否有任何编译器实际上拒绝了基说明符中类的typedef?GCC甚至检查基类类型是否不是const,这改进了非标准功能。有解决办法吗?我唯一能想到的就是用C++11别名模板替换typedef。一个模板化的别名声明声明

c++ - 返回 T<A> 的函数在某些 typedef 情况下无法编译

我不明白为什么func3()无法编译,当func2()和func4()做。g++4.1.2:error:'B::my_t'hasincompletetypeVS2008:errorC2079:'B::my_t'usesundefinedclass'A'templatestructC{Tmt_t;};templatestructB{typedefCC_type;Tmy_t;};structOther{};structA{Bfunc2();B::C_typefunc3();//error:'B::my_t'hasincompletetypeB::C_typefunc4();};intmai

c++ - 在函数上使用 typedef

在下文中,我如何使用typedef语法定义我的函数?typedefvoidF();//declaremyfunctionFf;//errorFf{} 最佳答案 函数的定义将遵循通常的语法://declaremyfunctionFf;//itisexactlyequivalentto:voidf();//definitionvoidf(){cout要测试该定义确实是之前声明的函数的定义,只需调用函数f()>after声明和before定义(阅读main()中的注释)://declarationFf;intmain(){f();//at

模板化类中 typedef 的 C++ 错误

我正在尝试编写一个简单的模板化事件调度程序,但我遇到了我不理解的编译器错误,搜索它也没有任何帮助。我正在使用VisualStudio2013Express。这是我的代码:templateclassEventDispatcher{public:typedefvoid(EventHandler)(Tevent);EventDispatcher(){}~EventDispatcher(){}voidaddListener(conststd::stringeventName,EventHandlerhandler){}voidfireEvent(Tevent){}private:typedef