问题描述
说我有一个class Allocator
,其方法将以某种方式分配空间,在该空间中构造所需的对象,然后对所构造的对象执行某些操作。对象的类是异构的:我们可以在一个构建中调用多个类。简而言之,我需要一个如何写???的想法。此示例代码中的部分:
class Foo {
Foo( int i,double d,string s );
};
class Bar {
Bar( int i );
};
class Allocator {
template<typename T> Allocate( ??? );
}
template<typename T> Allocator::Allocate( ??? ) {
void* pv = /* memory is found somehow,perhaps using sizeof( T ) */;
T* pobj = new( pv ) T( ??? );
/* something is done with the new object */
}
/* user of this class would write something like this??? */
allocator.Allocate<Foo>( 1,2.3,"four" );
allocator.Allocate<Bar>( 1 );
我看过initializer_list<>
和参数包,但没有看到如何将它们与模板方法结合在一起。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)