草庐IT

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++ - C/C++ 下划线 t/type (_t/_type) 和类名?

我理解下划线t(_t)是用来标识类型的,下划线type(_type)也是,通常在typedef语句中.用法是否略有不同(例如,模板使用下划线类型,非模板使用下划线t)?为什么不在声明中使用它们?例如:classperson_t{};enumerror_t{};这与之前关于下划线t的问题不同,因为它还要求区分下划线类型。此外,(盲目地)很明显,这两个后缀只是约定俗成,但不清楚为什么它们都用于C++标准。例如,std::size_t与std::istream::pos_type。 最佳答案 对于您的第一个问题,我不知道有任何答案,我相信

c++ - 有没有办法为模板函数参数定义一个类型?

下面的代码应该表明我的意思是您是否可以在参数中使用模板化函数typedef...#includestructvec2{floatx,y;};structdvec2{doublex,y;};templatevoidfunction(std::vector&list,decltype(T::x)scalarVal,decltype(T::x)scalarVal2){typedefdecltype(T::x)scalarType;scalarTypea;//aisnowadoubleorfloatdependingontemplateargument}intmain(){std::vecto

c++ - 如何从 C++ 源代码中提取所有类型定义、结构和 union

我继承了一个包含数百个文件的VisualStudio项目。我想从每个.h/.cpp文件中提取所有类型定义、结构和union,并将结果放入一个文件中。每个typdef/struct/union都应该在结果文件中占一行。这将使排序变得容易得多。typdefintmyType;structmyFirstStruct{chara;intb;...};unionPart_Number_Serial_Number_Part_2_Response_Message_Type{struct{Message_Response_Head_TypeHead;Part_Num_Serial_Num_Part_2

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++ 字符串比较

#include"stdafx.h"#include"iostream"#include"string"usingnamespacestd;voidmain(){stringa="a";stringb(1,-70);/*constructor,createastringhaving1characterthatitsvalueisequalto-70*/couta)?b:a);}//outputonscreen:bwasprinted,nota(!)为什么b>a虽然b的值小于a的值?我该如何纠正这种情况? 最佳答案 在VS2010上,

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