0

I want to return an empty vector at the end of my function. Does the following all equivalent? How to understand each of them?

return vector<int>();
return vector<int>{};
return NULL;

The following is my understanding:

1.vector<int>() means creating an empty vector object, which is uninitialized so it's NULL.

2.vector<int>{} means creating an empty vector, which has a size 0.

Is there a difference between size 0 and NULL? Thanks a lot~

Lusha Li
  • 319
  • 3
  • 11
  • 5
    `NULL` has nothing to do at all with `std::vector`. What type is your function returning? – François Andrieux Jul 12 '18 at 13:55
  • 1
    `vector()`is not unitiliazed. You call default constructor here. And C++ is not Java, you can't `return NULL` in place of any object. – Yksisarvinen Jul 12 '18 at 13:56
  • 2
    Depending on your implementation, trying to return `NULL` in place of a vector *may* find `explicit std::vector::vector(std::size_t)` – Caleth Jul 12 '18 at 14:21
  • @François Andrieux, the return type is vector – Lusha Li Jul 13 '18 at 13:14
  • @LushaLi Then `return NULL;` shouldn't compile. Unlike some other languages, objects in c++ don't implicitly support a null state. – François Andrieux Jul 13 '18 at 13:15
  • @Yksisarvinen, thanks, I don't know C++ cannot return NULL since I have never tried. Do you mean we cannot explicitly return NULL but we can return vector(), which is not initialized(vector()=NULL) – Lusha Li Jul 13 '18 at 13:19
  • @François Andrieux, thanks. When return NULL will work? I did some search. It said it should never work for C++. If you really want to, we can return a nullptr. – Lusha Li Jul 13 '18 at 13:25
  • `vector()` is initialized and is fully functional object. It just doesn't contain any `int`s inside. If you want to learn C++, start with [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). It will be easier than trying to apply your experience from different languages :) – Yksisarvinen Jul 13 '18 at 13:26
  • @LushaLi `NULL` and `nullptr` will work for any function that has a pointer return type. I don't know where you saw that `NULL` never works, that's not true. Maybe you read that it shouldn't be used anymore, since `nullptr` does a better job. Neither will work with `std::vector` return types. – François Andrieux Jul 13 '18 at 13:28
  • @François Andrieux, thanks. I got it. My bad. It said they would never work for returning references. – Lusha Li Jul 13 '18 at 13:31

1 Answers1

6

The first two are both doing value initialization, and are the same.

NULL is the old backward-compatible symbolic constant for null pointers. C++ doesn't have the concept of "null" values. Unless you return a pointer, it's invalid.


Regarding NULL, in C++ before the C++11 standard you should really be using 0 for null pointers. And since the C++11 standard you should be using the nullptr literal.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621