Search code examples
c++c++11comparisoncomparison-operators

Possible meaning of x > y > new T : syntax


I was reading this article about the most useful C++11 features and I bumped into this chunk of code in the last section:

 if(_size != copy._size)
 {
    _buffer = nullptr;
    _size = copy._size;
    _buffer = _size > 0 > new T[_size] : nullptr;
 }

I was used to consider the last line as (_size > 0) > other_Value but in this case the right operand is a new declaration. I really cannot understand the sense of it. Also, what does the : nullptr refer to? Is there something which is initialised to nullptr? If yes, what?


Solution

  • I think, that it's wrong snippet. It should be

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

    that is basically ternary operator. If _size > 0, then memory for array of T of size _size will be allocated, in other case nullptr will be assigned to _buffer. And it's not C++11 feature, since in C++98 it can be just

    _buffer = _size > 0 ? new T[_size] : 0; // or NULL, or (void*)0.