这个主题出现在这个线程中,关于 Visual Studio 2015 的 std::list::sort() 的变化:
`std::list<>::sort()` - why the sudden switch to top-down strategy?
新版本的 std::list::sort 不需要默认可构造的 std::list,因为它只使用迭代器,并且不创建任何本地列表,所以列表是否可以并不重要'不是默认构造的。之前的版本使用本地列表(注意 - 列表的每个实例都涉及一个哨兵节点的动态分配):
typedef list<_Ty, _Alloc> _Myt;
// ...
const size_t _MAXBINS = 25;
_Myt _Templist, _Binlist[_MAXBINS];
我正在尝试创建一个非默认的可构造列表,使用 Visual Studio 2015 版本来测试对 std::list::sort() 的更改如何处理此问题。
首先,我尝试了 Microsoft C++ 11 最小分配器示例。 更新 - 我不得不更改一行以使 Jonathan Wakely 的回答生效并证明问题:
template <class T>
struct Mallocator
{
typedef T value_type;
// Mallocator() noexcept {} // replaced this line from the Microsoft example
Mallocator(T) noexcept {} // no default constructor
// A converting copy constructor:
template<class U> Mallocator(const Mallocator<U>&) noexcept {}
template<class U> bool operator==(const Mallocator<U>&) const noexcept
{
return true;
}
template<class U> bool operator!=(const Mallocator<U>&) const noexcept
{
return false;
}
T* allocate(const size_t n) const;
void deallocate(T* const p, size_t) const noexcept;
};
template <class T>
T* Mallocator<T>::allocate(const size_t n) const
{
if (n == 0)
{
return nullptr;
}
if (n > static_cast<size_t>(-1) / sizeof(T))
{
throw std::bad_array_new_length();
}
void* const pv = malloc(n * sizeof(T));
if (!pv) { throw std::bad_alloc(); }
return static_cast<T*>(pv);
}
template<class T>
void Mallocator<T>::deallocate(T * const p, size_t) const noexcept
{
free(p);
}
更新 - Mallocator 更改为没有默认构造函数,这现在会导致编译错误:
typedef unsigned long long uint64_t;
std::list <uint64_t, Mallocator<uint64_t>> dll; // doubly linked list
使用来自 Jonathan Wakely 的建议更改工作并重现旧 std::list::sort 由于本地列表而出现编译错误的问题,并显示没有本地列表的新 std::list::sort 工作没有默认构造函数:
std::list<uint64_t, Mallocator<uint64_t>> dll(Mallocator<uint64_t>(0));
我也试过这个方法,基于这里的一个线程:
struct Allocator {
void construct(void* p, const void* container) const {};
void destruct(void* p, const void* container) const {};
};
void* operator new (size_t size, const Allocator& alloc, const void* container)
{
void* allocated_memory = std::malloc(size);
if (!allocated_memory) {
throw std::bad_alloc();
}
alloc.construct(allocated_memory, container);
return allocated_memory;
}
void operator delete(void* p, const Allocator& alloc, const void* container)
{
alloc.destruct(p, container);
std::free(p);
}
主要
typedef unsigned long long uint64_t;
// ...
Allocator alloc;
std::list<uint64_t> *dll = new(alloc, NULL)std::list<uint64_t>;
// ...
operator delete(dll, alloc, NULL);
但这对 std::list::sort 的旧版本和新版本都有效,因此它获得了默认构造函数。
所以问题是如何创建一个非默认的可构造分配器?
感谢 Igor Tandetni 的演示和 Jonathan Wakely 的回答,我能够将上面的 Microsoft 示例分配器(在评论中注明)更改为没有默认构造函数,并重现与旧 std::相关的问题列表::排序。
最佳答案
如果您默认构造一个std::list,那么它将默认构造它的分配器,所以这个变量定义仍然需要一个默认构造函数:
std::list <uint64_t, Mallocator<uint64_t>> dll; // doubly linked list
如果你想在没有默认构造函数的情况下测试分配器,你需要以不同的方式进行,例如
std::list <uint64_t, Mallocator<uint64_t>> dll(Mallocator<uint64_t>(args));
或者:
Mallocator<uint64_t> alloc(some, args, for, your, allocator);
std::list <uint64_t, Mallocator<uint64_t>> dll(alloc);
关于c++ - 如何创建 C++ 11 非默认可构造分配器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40683637/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我在MiniTest::Spec和Capybara中使用以下规范:find_field('Email').must_have_css('[autofocus]')检查名为“电子邮件”的字段是否具有autofocus属性。doc说如下:has_css?(path,options={})ChecksifagivenCSSselectorisonthepageorcurrentnode.据我了解,字段“Email”是一个节点,因此调用must_have_css绝对有效!我做错了什么? 最佳答案 通过JonasNicklas得到了答案:No