我一直在使用 Howard Hinnant 的 stack allocator它就像一个魅力,但实现的一些细节对我来说有点不清楚。
new和 delete用过的? allocate()和 deallocate()成员函数使用 ::operator new和 ::operator delete分别。同样,成员函数 construct()使用全局布局 new。为什么不允许任何用户定义的全局或特定类重载?std::alignment_of<T> ?max_size有一个throw()异常规范?这不气馁吗(例如,参见更有效的 C++ 第 14 条)?当分配器发生异常时,是否真的需要终止和中止?这是否会随着新的 C++11 noexcept 而改变?关键字?construct()成员函数将是完美转发(到被调用的构造函数)的理想候选者。这是编写符合 C++11 的分配器的方法吗?最佳答案
I've been using Howard Hinnant's stack allocator and it works like a charm, but some details of the implementation are a little unclear to me.
很高兴它一直在为你工作。
1. Why are global operators
newanddeleteused? Theallocate()anddeallocate()member functions use::operator newand::operator deleterespectively. Similarly, the member functionconstruct()uses the global placement new. Why not allow for any user-defined global or class-specific overloads?
没有什么特别的原因。随意以最适合您的方式修改此代码。这只是一个例子,它绝不是完美的。唯一的要求是分配器和释放器提供正确对齐的内存,并且构造成员构造一个参数。
在 C++11 中,构造(和销毁)成员是可选的。如果您在提供 allocator_traits 的环境中操作,我建议您将它们从分配器中删除。 .要找出答案,只需删除它们,看看是否还能编译。
2. Why is alignment set to hard-coded 16 bytes instead of
std::alignment_of<T>?
std::alignment_of<T>可能会正常工作。那天我可能是偏执狂。
3. Why do the constructors and
max_sizehave athrow()exception specification? Isn't this discouraged (see e.g. More Effective C++ Item 14.)? Is it really necessary to terminate and abort when an exception occurs in the allocator? Does this change with the new C++11noexceptkeyword?
这些成员永远不会扔。对于 C++11,我应该将它们更新为 noexcept .在 C++11 中,使用 noexcept 装饰事物变得更加重要。 ,尤其是特殊成员。在 C++11 中,可以检测表达式是否为 nothrow。代码可以根据该答案进行分支。已知为 nothrow 的代码更有可能导致通用代码分支到更有效的路径。 std::move_if_noexcept是 C++11 中的典型例子。
不要使用 throw(type1, type2)曾经。它已在 C++11 中被弃用。
请使用 throw()当你真的想说:这永远不会抛出,如果我错了,终止程序以便我可以调试它。 throw()在 C++11 中也已弃用,但有一个替代品:noexcept .
4. The
construct()member function would be an ideal candidate for perfect forwarding (to the constructor that is being called). Is this the way to write C++11 conformant allocators?
是的。然而allocator_traits会为你做的。让它。 std::lib 已经为您调试了该代码。 C++11 容器将调用 allocator_traits<YourAllocator>::construct(your_allocator, pointer, args...) .如果你的分配器实现了这些函数,allocator_traits 将调用你的实现,否则它会调用经过调试的、高效的默认实现。
5. What other changes are necessary to make the current code C++11 conformant?
说实话,这个分配器并不真正符合 C++03 或 C++11。当您复制分配器时,原件和拷贝应该彼此相等。在这个设计中,这绝不是真的。然而,这件事在许多情况下仍然只是碰巧起作用。
如果你想让它严格符合,你需要另一个间接级别,这样拷贝将指向同一个缓冲区。
除此之外,C++11 分配器所以比 C++98/03 分配器更容易构建。这是您必须做的最低要求:
template <class T>
class MyAllocator
{
public:
typedef T value_type;
MyAllocator() noexcept; // only required if used
MyAllocator(const MyAllocator&) noexcept; // copies must be equal
MyAllocator(MyAllocator&&) noexcept; // not needed if copy ctor is good enough
template <class U>
MyAllocator(const MyAllocator<U>& u) noexcept; // requires: *this == MyAllocator(u)
value_type* allocate(std::size_t);
void deallocate(value_type*, std::size_t) noexcept;
};
template <class T, class U>
bool operator==(const MyAllocator<T>&, const MyAllocator<U>&) noexcept;
template <class T, class U>
bool operator!=(const MyAllocator<T>&, const MyAllocator<U>&) noexcept;
您可以选择考虑制作 MyAllocator可交换并将以下嵌套类型放入分配器中:
typedef std::true_type propagate_on_container_swap;
还有一些其他类似的旋钮可以在 C++11 分配器上进行调整。但是所有的旋钮都有合理的默认值。
更新
上面我注意到我的stack allocator由于拷贝不相等,因此不符合要求。我决定将此分配器更新为符合 C++11 的分配器。新的分配器称为 short_allocator并记录在 here .
short_allocator不同于 stack allocator因为“内部”缓冲区不再是分配器内部的,而是一个单独的“竞技场”对象,可以位于本地堆栈上,或者给定线程或静态存储持续时间。 arena虽然不是线程安全的,但请注意这一点。如果你愿意,你可以让它成为线程安全的,但这会带来 yield 递减(最终你会重新发明 malloc)。
这是符合要求的,因为分配器的拷贝都指向同一个外部 arena .注意N的单位现在是字节,而不是 T 的数量.
可以通过添加 C++98/03 样板(typedef、构造成员、销毁成员等)将此 C++11 分配器转换为 C++98/03 分配器。一项乏味但简单的任务。
新 short_allocator 的这个问题的答案保持不变。
关于c++ - 关于 Hinnant 的栈分配器的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11648202/
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
我在MiniTest::Spec和Capybara中使用以下规范:find_field('Email').must_have_css('[autofocus]')检查名为“电子邮件”的字段是否具有autofocus属性。doc说如下:has_css?(path,options={})ChecksifagivenCSSselectorisonthepageorcurrentnode.据我了解,字段“Email”是一个节点,因此调用must_have_css绝对有效!我做错了什么? 最佳答案 通过JonasNicklas得到了答案:No
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
通过rubykoans.com,我在about_array_assignment.rb中遇到了这两段代码你怎么知道第一个是非并行赋值,第二个是一个变量的并行赋值?在我看来,除了命名差异之外,代码几乎完全相同。4deftest_non_parallel_assignment5names=["John","Smith"]6assert_equal["John","Smith"],names7end45deftest_parallel_assignment_with_one_variable46first_name,=["John","Smith"]47assert_equal'John
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search
由于fast-stemmer的问题,我很难安装我想要的任何rubygem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=
当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub
我早就知道Ruby中的“常量”(即大写的变量名)不是真正常量。与其他编程语言一样,对对象的引用是唯一存储在变量/常量中的东西。(侧边栏:Ruby确实具有“卡住”引用对象不被修改的功能,据我所知,许多其他语言都没有提供这种功能。)所以这是我的问题:当您将一个值重新分配给常量时,您会收到如下警告:>>FOO='bar'=>"bar">>FOO='baz'(irb):2:warning:alreadyinitializedconstantFOO=>"baz"有没有办法强制Ruby抛出异常而不是打印警告?很难弄清楚为什么有时会发生重新分配。 最佳答案
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www