2
爲什麼不能正常工作?boost :: const成員可選
struct O {
O(int i, int j)
: i(i)
, j(j)
{}
int const i;
int const j;
};
int main(int argc, char** argv)
{
boost::optional<O> i;
i.reset(O(4, 5));
return 0;
}
它似乎試圖使用賦值運算符而不是試圖構建它。我假定它會在未初始化的內存上調用O的拷貝構造函數。
/..../include/boost/optional/optional.hpp:433:69: error: use of deleted function ‘O& O::operator=(const O&)’
.... error: ‘O& O::operator=(const O&)’ is implicitly deleted because the default definition would be ill-formed:
.... error: non-static const member ‘const int O::i’, can’t use default assignment operator
.... error: non-static const member ‘const int O::j’, can’t use default assignment operator
在哪些情況下會使用它?當我嘗試分配給可選項時? – nishantjr
@njr:當賦值給一個已經存在值的'Optional'時,將使用賦值運算符。 –
Ahhh ...如果我重置了兩次,它會失敗 – nishantjr