std::allocator_traits当我提供一个带有单个模板参数的分配器的 STL 样式容器时,它会自动发挥它的魔力,但当我提供一个带有两个模板参数但其他方面相似的分配器的 STL 样式容器时,它不会自动发挥作用。
我需要做什么来告诉std::allocator_traits如何与具有多个模板参数的分配器交互?是否有可能获得 std::allocator_traits在这种情况下提供合理的默认值?
例如,如果我采用 Howard Hinnant 在 Allocator Boilerplate 中提供的简单分配器并将其提供给 std::vector<>那么一切都很好。如果我添加一个虚拟 int allocator 的参数模板(并根据需要进行轻微修改)然后我得到编译器错误,因为编译器找不到 rebind ,等等。
这是代码中的描述:
http://coliru.stacked-crooked.com/a/173c57264137a351
如果我必须专攻std::allocator_traits我自己在这种情况下,有没有办法仍然获得默认值?
最佳答案
标准只提供默认的rebind对于具有多个模板 type 参数的分配器:
17.6.3.5 Allocator requirements [allocator.requirements]
3 Note A: The member class template
rebindin the table above is effectively a typedef template. [ Note: In general, if the nameAllocatoris bound toSomeAllocator<T>, thenAllocator::rebind<U>::otheris the same type asSomeAllocator<U>, whereSomeAllocator<T>::value_typeisTandSomeAllocator<U>:: value_typeisU. — end note ] IfAllocatoris a class template instantiation of the formSomeAllocator<T, Args>, whereArgsis zero or more type arguments, andAllocatordoes not supply arebindmember template, the standard allocator_traits template usesSomeAllocator<U, Args>> in place ofAllocator:: rebind<U>::otherby default. For allocator types that are not template instantiations of the above form, no default is provided.
由于您有一个非类型 ( int) 参数,因此没有提供默认值。修复很简单:只需将您自己的重新绑定(bind)添加到您的分配器。
template<class T, int I>
class allocator_w_int
{
// as before
template<class U>
struct rebind { using other = allocator_w_int<U, I>; };
};
关于允许 Allocator<T, Args...> 形式的分配器的基本原理但不适用于 Alloc<T, Ns...> 形式的分配器,只能猜测,但这也会导致过多的Alloc<T, Args.., Ns...>等等等等。这就是为什么模板元编程库(例如 Boost.MPL)总是包装它们的非类型参数 N类型 T里面的东西像integral_constant<T, N> .这也是你的路线,通过定义
template<class T, class Arg>
class allocator_w_int; // leave undefined
template<int N>
using int_ = std::integral_constant<int, N>;
template<class T, int I>
class allocator_w_int<T, int_<I>>
{
// replace all occurances of I, J --> int_<I>, int_<J>
};
关于c++ - std::allocator_traits 默认为具有多个模板参数的分配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34861283/
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"
我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2