草庐IT

non-struct

全部标签

c++ - C/C++ : Packing or padding data in a struct

我正在使用带有GNUGCC编译器的Code::BlocksIDE。structtest{chara;chare;charf;charb;chard;};sizeof(test)返回5。我读了这个答案:Whyisn'tsizeofforastructequaltothesumofsizeofofeachmember?为什么最后一个char后没有填充,所以sizeof(test)返回6或8?添加short和int等后,我可以问更多的问题。但我认为这个问题现在很好。填充不会使处理器更容易处理结构吗? 最佳答案 char的对齐方式仅为1,因

c++ - 将前向声明的 C-struct 定义为 C++-struct

将struct前向声明为C-struct是否合法?//api.h#ifdef__cplusplusextern"C"{#endiftypedefstructhandle_taghandle_t;handle_t*construct();voiddestruct(handle_t*h);voidfunc(handle_t*h);#ifdef__cplusplus}#endif然后将其定义为C++-struct,即非POD类型?//api.cppstructhandle_tag{voidfunc();std::stringmember;};voidfunc(handle_t*h){h->f

c++ - C/C++ : Pointers within Const Struct

如何在函数fn中强制obj->val1指向的内存的常量性?#includestructfoo{int*val1;int*val2;int*val3;};voidfn(constfoo*obj){//Idon'twanttobeabletochangetheintegerthatval1pointsto//obj->val1=newint[20];//Ican'tchangethepointer,*(obj->val1)=20;//ButIcanchangethememoryitpointsto...}intmain(intargc,char*argv[]){//Ineedtobeabl

c++ - BOOST_FUSION_ADAPT_STRUCT 的限制

我尝试使用BOOST_FUSION_ADAPT_STRUCT宏并尝试了一些简单的操作,例如使用Fusion打印任意结构。从此examplecodegiveninthedocumentation开始,我无法在我的改编结构上执行融合序列允许的一些操作。#include#include#include#include#includenamespacefuz=boost::fusion;namespacedemo{structemployee{std::stringname;intage;};}//demo::employeeisnowaFusionsequenceBOOST_FUSION_A

x86-64 : when are structs/classes passed and returned in registers? 上的 C++

假设Linux上的x86-64ABI,在C++中的什么条件下,结构传递给函数是在寄存器中还是在堆栈中?在什么情况下它们会返回到寄存器中?类(class)的答案会改变吗?如果它有助于简化答案,您可以假设一个参数/返回值并且没有浮点值。 最佳答案 ABI规范定义here.有更新版本可用here.我假设读者已经习惯了文档的术语,并且他们可以对基本类型进行分类。如果对象大小大于两个八字节,则在内存中传递:structfoo{unsignedlonglonga;unsignedlonglongb;unsignedlonglongc;//Com

c++ - 为什么不能实例化带有 "non const"复制构造函数的对,而没有实例化一对是可能的?

假设您有以下类(class):structA{A(){}A(A&)=delete;};intmain(){std::pairp1;return0;}以下代码将无法编译(使用-std=c++11和g++)并出现以下错误:/usr/include/c++/5/bits/stl_pair.h:Ininstantiationof‘structstd::pair’:test.cpp:13:23:requiredfromhere/usr/include/c++/5/bits/stl_pair.h:127:17:error:‘constexprstd::pair::pair(conststd::pa

c++ - 错误 : invalid initialization of non-const reference of type ‘bool&’ from an rvalue of type ‘std::vector<bool>::reference {aka std::_Bit_reference}’

为什么我会收到错误:从类型为“std::vector::reference{akastd::_Bit_reference}”的右值对类型为“bool&”的非常量引用进行无效初始化?vector>vis;bool&visited(intx,inty){returnvis[x][y];//error}据我所知,vector中的operator[]返回引用,所以它应该是一个左值,但它不起作用。我应该怎么做才能让它发挥作用? 最佳答案 那是因为std::vector不是它看起来的样子。std::vector有一个特化与类型bool-它是空间

c++ - 这个模式 : using a struct to contain a single method 有什么意义

在我们的代码中,我们有很多这种模式的情况:classouterClass{structinnerStruct{wstringoperator()(wstringvalue){//dosomethingreturnvalue;}};voiddoThing(){wstringinitialValue;wstringfinalValue=innerStruct()(initialValue);}};这样做的好处是什么:classouterClass{wstringchangeString(wstringvalue){//dosomethingreturnvalue;}voiddoThing(

c++ - 铿锵错误 : non-type template argument refers to function that does not have linkage -- bug?

我有一些非常简单的(C++11)代码,最新的clang(version3.4trunk187493)无法编译,但GCC编译正常。代码(下面)实例化函数模板foo使用局部函数类型Bar然后尝试将其地址用作类模板Func的非类型模板参数:templatestructFunc{};templateexterninlinevoidfoo(){usingFoo=Func>;}intmain(){structBar{};//function-localtypefoo();return0;}clang发出以下错误:error:non-typetemplateargumentreferstofunct

c++ - 仿函数 : templated struct vs templated operator()

标准库函数对象的通常模式是拥有一个带有非模板operator()的模板化结构。例如,std::less看起来像这样:templatestructless{booloperator()(constT&lhs,constT&rhs)const{returnlhsvec=...;std::sort(vec.begin(),vec.end(),less{});我的问题是,为什么这比具有模板化operator()的非模板结构更好?看起来上面的仿函数在操作上等同于:structless2{templatebooloperator()(constT&lhs,constT&rhs)const{retu