Cant understand this syntax

What does the last line actually do ? Is this syntax valid for unique_ptr only ?

1
2
3
4
  std::unique_ptr<T[]> _buffer(new T[16]);
  int _size(16);

  _buffer = _size > 0 > new T[_size] : nullptr;


Are you sure the second > is not a question mark?

 
_buffer = _size > 0 ? new T[_size] : nullptr;

If that's so, it means that if _size > 0 then return new T[_size], otherwise return nullptr.

1
2
3
4
if (_size > 0)
	_buffer = new T[_size];
else
	_buffer = nullptr;
1) This line is not valid at all. I think you meant _buffer = _size > 0 ? new T[_size] : nullptr;
2) Even in this case it is still not valid: you cannot assign pointer to smart pointer, you need to expicitely reset pointer: _buffer.reset(_size > 0 ? new int[_size] : nullptr);

3) ?: construct is a ternary operator:
http://www.cprogramming.com/reference/operators/ternary-operator.html
http://www.tutorialspoint.com/cplusplus/cpp_conditional_operator.htm
http://en.wikipedia.org/wiki/%3F:#C.2B.2B
I've copied this line directly from the article about c++11 features
http://www.codeproject.com/Articles/570638/Ten-Cplusplus-Features-Every-Cplusplus-Developer

Thanks for clearing that up
Topic archived. No new replies allowed.