草庐IT

scoped-storage

全部标签

c++ - 为什么 std::aligned_storage 的分配总是有一个偏移量?

分配std::aligned_storage::type时在堆上,我总是得到一个偏移16个字节的指针(在x64上;在x86上它偏移8个字节)。换句话说,这:#include#includeintmain(){typedefstd::aligned_storage::typeMemPage;MemPage*p_mp=newMemPage;std::cout给我(例如)0x72f010尽管我希望最后三位数字全部为零。分配std::aligned_storage::type时在堆栈上,一切都按预期工作。我在ubuntu14.04上使用gcc-4.8.2x86_64。

c++ - Qt错误: `qApp' was not declared in this scope

据我所知,qApp是全局指针,因此它应该可以在任何地方访问,但我收到此错误error:qAppwasnotdeclaredinthisscope。1#include"textEdit.h"23TextEdit::TextEdit(){4}56voidTextEdit::insertFromMimeData(constQMimeData*source){7if(qApp->mouseButtons()==Qt::MidButton){8return;9}10QTextEdit::insertFromMimeData(source);11}1213 最佳答案

c++ - 带有 C++ 模板的虚假 "use of local variable with automatic storage from containing function"?

以下代码无法在g++7.2.0中编译templateclassRequest{intcontent=0;public:friendvoidsetContent(inti,void*voidptr){Request*ptr=(Request*)voidptr;ptr->content=i;}intgetContent(){returncontent;}};intmain(){Requestreq;setContent(4,&req);returnreq.getContent();}有错误test.cpp:Ininstantiationof‘voidsetContent(int,void*

c++ - 等价于 C 中的 std::aligned_storage<>?

在C语言中,有没有一种方法可以使堆栈上的存储过度对齐(即比从类型系统推断出的对齐更多)?对于动态分配的内存中的变量,如果所有其他方法都失败了,我们总是可以手动对齐,但是对于自动分配的内存中的变量可以做什么呢?我想可以使用char[size+alignment-1]然后总是使用位操作来访问变量,但这似乎比必要的“有点”暗淡(harharhar;)). 最佳答案 在C2011中,有_Alignas和_Alignof关键字,标题这使得它们的使用稍微不那么难看,类型max_align_t(在中)。例如,你可以写double_Alignas(

c++ - 初始化 boost::scoped_ptr 数组的正确方法?

我有一个类,其中有一个范围指针数组,这些指针指向没有默认构造函数的对象。我发现“初始化”它们的唯一方法是像这样使用swap():classBar{Bar(char*message){};}classFoo{boost::scoped_ptrarr[2];Foo(){arr[0].swap(boost::scoped_ptr(newBar("ABC")));arr[1].swap(boost::scoped_ptr(newBar("DEF")));};}这感觉有点冗长和笨拙。我错过了更聪明的方法吗? 最佳答案 arr[0].reset

c++ - std::align 和 std::aligned_storage 用于内存块的对齐分配

我正在尝试分配一个大小为size的内存块,它需要Alignment对齐,而在编译时可能未定义大小。我知道存在_aligned_alloc、posix_memalign、_mm_alloc等例程,但我不想使用它们,因为它们会降低代码的可移植性。C++11提供了一个例程std::align和一个类std::aligned_storage,我可以从中检索POD类型进行分配一个将符合我的要求的元素。然而,我的目标是创建一个分配器,它将分配一个size大小的内存块(不仅仅是单个元素),该内存块将被对齐。这可能使用std::align吗?我问的原因是因为std::align移动指针,使用该指针的类

c++ - 在带有 std::unordered_map 的 std::scoped_allocator_adaptor 中使用自定义分配器

我正在尝试将一个简单的内存池分配器与std::unordered_map一起使用。我在std::string和std::vector中似乎成功地使用了同一个分配器。我希望unordered_map(和vector)中包含的项目也使用此分配器,因此我将我的分配器包装在std::scoped_allocator_adaptor中。简化定义集:templateusingmm_alloc=std::scoped_allocator_adaptor>;usingmm_string=std::basic_string,mm_alloc>;usingmm_vector=std::vector>;us

c++ - -O1/2/3 与 -std=c++1y/11/98 - 如果包含 <cmath> 我收到错误 : '_hypot' was not declared in this scope

我刚刚使用mingw-get-setup更新了MinGW而且我无法构建包含的任何内容header如果我使用大于-O0的东西与-std=c++1y.(我也试过c++11和c++98)我收到这样的错误:g++.exe-pedantic-errors-pedantic-Wextra-Wall-std=c++1y-O3-cZ:\Projects\C++\L6\src\events.cpp-oobj\src\events.oInfileincludedfromz:\lander\mingw\lib\gcc\mingw32\4.8.1\include\c++\cmath:44:0,fromZ:\P

c++ - std::optional 实现为 union vs char[]/aligned_storage

在阅读GCC对std::optional的实现时,我注意到了一些有趣的事情。我知道boost::optional实现如下:templateclassoptional{//...private:boolhas_value_;aligned_storagestorage_;}但是libstdc++和libc++(以及Abseil)都像这样实现它们的可选类型:templateclassoptional{//...private:structempty_byte{};union{empty_byteempty_;Tvalue_;};boolhas_value_;}在我看来,它们在功能上是相同的

c++ - 错误消息 : name lookup of ‘jj’ changed for ISO ‘for’ scoping,(如果您使用 ‘-fpermissive’,G++ 将接受您的代码)

错误是:Infunction‘intreturnShortestWeightedBranch(std::vector>*)’:error:namelookupof‘jj’changedforISO‘for’scopingnote:(ifyouuse‘-fpermissive’G++willacceptyourcode)代码是:for(inti=0;i这里可能是什么问题?编辑1:我更改了以下内容:for(intjj=0;jj到:intjj;for(jj=0;jj现在它正在工作!!我不明白原因。 最佳答案 内部for语句的末尾有一个分号