【发布时间】:2020-08-24 15:48:57
【问题描述】:
假设我有一个不可复制和不可移动的类Person:
struct Person
{
int m_icNum;
explicit Person(int icNum) : m_icNum(icNum) {}
Person (const Person & other) = delete;
Person (Person && other) = delete;
};
还有一个班级PersonContainer:
struct PersonContainer
{
boost::optional<Person> m_person;
explicit PersonContainer(int icNum)
: m_person( icNum >= 0 ? Person(icNum) : boost::none) // this does not compile because the two operands of ternary operator ? must have the same type
{}
};
显然我无法在初始化列表中使用三元表达式构造m_person。另一种方法是使用boost::in_pace 在ctor 主体中构造它,但我想知道是否有一种很好的方法可以在初始化列表中构造它。
【问题讨论】:
标签: c++