草庐IT

auto-size

全部标签

c++ - 'Natural Size' 在 C++ 中的真正含义是什么?

我知道“自然大小”是指特定硬件处理效率最高的整数宽度。在数组或算术运算中使用short时,必须先将short整数转换为int。问:究竟什么决定了这个“自然大小”?我不是在寻找简单的答案,例如Ifithasa32-bitarchitecture,it'snaturalsizeis32-bit我想了解为什么这是最有效的,以及为什么一个短必须在对其进行算术运算之前进行转换。奖励问题:对long整数进行算术运算时会发生什么情况? 最佳答案 一般来说,每个计算机体系结构的设计都使得特定类型大小提供最有效的数字运算。具体大小则取决于体系结构,编

c# - 什么等于 c# 中的 c++ size_t

我在C++中有一个结构:structsome_struct{uchar*data;size_tsize;}我想在manged(c#)和native(c++)之间传递它。C#中的size_t是什么?附言我需要大小完全匹配,因为任何字节差异都会在包装时导致巨大的问题编辑:原生代码和托管代码都在我的完全控制之下(我可以随意编辑) 最佳答案 没有与size_t等效的C#。C#sizeof()operator无论平台如何,总是返回一个int值,因此从技术上讲,size_t的C#等价物是int,但这对您没有帮助。(注意Marshal.SizeO

c++ - 为什么这个 auto_ptr 的 dynamic_cast 会失败?

#include"iostream"classA{private:inta;public:A():a(-1){}intgetA(){returna;}};classA;classB:publicA{private:intb;public:B():b(-1){}intgetB(){returnb;}};intmain(){std::auto_ptra=newA();std::auto_ptrb=dynamic_cast>(a);return0;}错误:不能dynamic_cast`(&a)->std::auto_ptr::get()const 最佳答案

c++ - 如何实现 std::auto_ptr 是复制构造函数?

回到我的疯狂AutoArraythingy...(从那里引用重要的部分:classAutoArray{void*buffer;public://CreatesanewemptyAutoArrayAutoArray();//std::auto_ptrcopysemanticsAutoArray(AutoArray&);//Noteitcan'tbeconstbecausethe"other"reference//isnull'doncopy...AutoArray&operator=(AutoArray);~AutoArray();//Nothrowswap//Note:Atthemom

c++ - 为什么 vector.push_back(auto_ptr) 无法编译?

我了解到STL可以禁止程序员将auto_ptr放入容器中。例如下面的代码不会编译:auto_ptra(newint(10));vector>v;v.push_back(a);auto_ptr有拷贝构造函数,为什么这段代码还能通过? 最佳答案 查看thedefinitionofstd::auto_ptr:namespacestd{templatestructauto_ptr_ref{};templateclassauto_ptr{public:typedefXelement_type;//20.4.5.1construct/copy/

c++ - i + 1 < vec.size() 和 i < vec.size() - 1 之间的区别

在编程时我发现我的代码在使用条件i时出现运行时错误但对i+1工作正常.这里vec是一个空的std::vector.//givingerrorvectorvec;for(inti=0;ivec;for(inti=0;i+1 最佳答案 std::vector::size方法返回一个未签名的std::size_t。因此,如果它为空,您将得到0-1,但表示为无符号数,根据two'scomplement,它将下溢并变为18446744073709551615. 关于c++-i+1 h

c++ - vector<auto> 是不允许的吗? (错误 : invalid use of ‘auto’ )

我有:#include#includeusingnamespacestd;intmain(){autoa=-SOME_CONST_MAX;vectormyVec{a,a,a,a};}我不知道SOME_CONST_MAX的类型但我想制作一个-SOME_CONST_MAX类型的vector.我假设vector会起作用,因为它会从a的类型推导出来.我正在运行这些错误:g++-std=c++14main.cppmain.cpp:9:9:error:invaliduseof‘auto’vectormyVec{a,a,a,a};^main.cpp:9:13:error:templateargume

c++ - 为什么 auto 不允许作为函数参数?

来自thisquestion,显然auto不能用作函数参数。我的问题是为什么允许返回类型为auto但函数参数不是?autofunction(autodata){//DOESsomething}因为,thereauto在c++1z中有很多好处,那为什么不呢? 最佳答案 此语法是在ConceptsTS中提出的,但并未将其纳入C++17forvariousreasons.尽管我在下面概述了一些批评,但它已添加到C++20中。注意:通过将P1141R2合并到标准中,答案的以下部分已过时。我会把它留在这里供引用。然而,即使我们最终在下一次迭代

c++ - std::array::max_size 和 std::array::size 给出不同结果的示例

每当我尝试使用std::array的max_size()和size()函数时,我都会得到相同的结果,我想知道是否会出现其中两个给出不同结果的情况。 最佳答案 该函数的存在是为了与std::vector等其他容器兼容。对于std::array,这两个值将始终相同。 关于c++-std::array::max_size和std::array::size给出不同结果的示例,我们在StackOverflow上找到一个类似的问题: https://stackoverfl

c++ - 将 shared_ptr 转换为 auto_ptr?

我需要在我的代码中从shared_ptr获取auto_ptr。我可以进行反向操作——将auto_ptr转换为shared_ptr,因为shared_ptr具有这样的构造函数:templateexplicitshared_ptr(std::auto_ptr&r);我可以将shared_ptr转换为auto_ptr吗?还是设计上不可能? 最佳答案 这在设计上是不可能的,因为该对象可能与其他共享指针共享,因此将其“提取”到auto_ptr可能会导致删除引用的对象。出于同样的原因,shared_ptr没有像auto_ptr那样的“relea