有条件地构造参数包的默认可构造类
Conditionally construct default-constructible classes of a parameter pack
我有一个模板类Container
,其中模板构造函数定义如下:
template<class... Objects>
class Container
{
public:
template<class... Objs>
Container(Objs*... objs)
{
// initialize all pointers of m_objs to nullptr
objs_init<Objects...>();
// initialize the pointers of m_objs corresponding to parameters pack objs...
objs_init<Objs...>(objs...);
}
private:
template<class Obj, class O1, class ...On>
void objs_init()
{
objs_init<Obj>();
objs_init<O1, On...>();
}
template<class Obj, class O1, class ...On>
void objs_init(Obj* obj, O1* o1, On*... on)
{
objs_init<Obj>(obj);
objs_init<O1, On...>(o1, on...);
}
template<class Obj>
void objs_init(Obj* obj = nullptr)
{
std::get<Obj*>(m_objs) = obj;
}
std::tuple<Objects*...> m_objs;
};
和一些类A
,B
,C
,...定义如下:
class A { public: A(bool) {} };
class B { /*trivial default ctor*/ };
class C { public: C(int) {} };
如果我以这种方式创建一个Container<A,B,C>
对象:
Container<A,B,C> cnt(new A(true), new C(1));
假设模板参数中指定的所有类都不同,则Container<>
模板构造函数按预期工作,即:
m_objs
元组的所有指针首先设置为nullptr
,然后m_objs
的第一个(A*
(和第三个(C*
(指针分别设置为new A(true)
和new C(1)
返回的值。
现在,我希望Container<>
构造函数默认构造m_objs
的每个未初始化的指针。但我不知道如何实现这一目标。
我尝试将以下模板成员函数添加到Container<>
:
template<class Obj, class O1, class ...On>
void objs_ctor()
{
objs_ctor<Obj>();
objs_ctor<O1, On...>();
}
template<class Obj>
void objs_ctor()
{
Obj*& obj = std::get<Obj*>(m_objs);
if (obj == nullptr)
obj = new Obj;
}
以及在构造函数Container<>
调用objs_ctor<>
:
// default-construct unitialized pointers of `m_objs`
objs_ctor<Objects...>();
但是,这显然不起作用,因为我实现objs_ctor<>
的方式要求参数包的每个类都存在默认构造函数(我不想这样做(;所以我收到以下编译器错误:
error C2512: 'A' : no appropriate default constructor available
为了使其正常工作,我还需要检查我尝试自动创建的对象是否是默认可构造的。我认为这可以使用std::is_default_constructible<>
来实现,但我不知道如何正确使用它。
感谢您的帮助。
假设。
在构造函数中添加对objs_ctors<Objects...>();
的调用
template<class... Objs>
Container(Objs*... objs)
{
objs_init<Objects...>();
objs_init<Objs...>(objs...);
objs_ctors<Objects...>();
}
并以这种方式定义objs_ctor<>()
和objs_ctors<>()
template <typename Obj>
typename std::enable_if<true == std::is_default_constructible<Obj>::value, void>::type objs_ctor ()
{
Obj * & obj = std::get<Obj*>(m_objs);
if ( obj == nullptr)
obj = new Obj;
}
template <typename Obj>
typename std::enable_if<false == std::is_default_constructible<Obj>::value, void>::type objs_ctor ()
{ }
template <typename Obj, typename O1, typename ... On>
void objs_ctors()
{
objs_ctor<Obj>();
objs_ctors<O1, On...>();
}
template <typename Obj>
void objs_ctors ()
{
objs_ctor<Obj>();
}
请注意:我想你的例子
Obj*& obj = std::get<Obj*>(m_objs);
if (obj != nullptr)
obj = new Obj;
错了。
我想你的意图是修改(分配(obj
当obj
是nullptr
时,所以
Obj*& obj = std::get<Obj*>(m_objs);
if (obj == nullptr)
obj = new Obj;
PS:对不起,我的英语不好。
相关文章:
- 如何使用默认参数等选择模板专业化
- 当函数模板参数是具有默认参数的类模板时,函数模板参数的推导如何执行
- 何时提供默认参数作为模板参数
- 将const引用参数初始化为默认参数会导致悬空引用吗
- 如何修复带有 clang 的参数'args'缺少默认参数的问题?
- 函数不接受 X 参数,函数使用默认参数
- initializer_list具有默认参数的构造函数
- C++ 带有默认参数的结构,可选择在构造函数中更改
- 为什么C++构造函数在继承中需要默认参数?
- 必须非常量别名参数及其默认参数常量
- C++ 默认参数使用其他参数
- 组合函数参数包和默认参数
- 如何定义 Vector2f 的默认参数?[SFML]
- 有条件地将默认参数传递给函数(使用"?"运算符)
- 使用具有默认参数的函数模板进行 decltype 会使结果混乱(一个有趣的问题或 gcc 的错误)
- 如何设置默认参数以防用户不输入另一个参数
- 结构 c++ 中的默认参数
- 构造函数委托与默认参数
- 默认参数和函数指针作为函数参数C++
- 如何在继承层次结构中调用具有默认参数的构造函数?