Point of #include <new>? Also, what is using std::cout?

So, one of the students I'm grading put the following statements at the top of his code. I'm honestly a little confused.
1
2
3
4
5
#include <iostream>
#include <cassert>
#include <cstddef>
#include <new>
using std::cout;


No one else in the class did this. He is implementing a Deque.

Firstly, what is the point of "new"? "new" is a reserved keyword. And new throws a bad_alloc exception by default. What more could he possibly be doing?

Second, using std::cout? Is that even valid? It's funny because this particular student doesn't even appear to be using the iostream in the file.

Are both #include <new> and using std::cout valid? Was it smart on his end to do this?
Last edited on
Firstly, what is the point of "new"?

http://stackoverflow.com/questions/2788388/when-is-include-new-library-required-in-c

Second, using std::cout? Is that even valid?


Yes. This means you do not have to write std::cout and can instead just write cout. But you'll still need to write std::cin and std::endl for example.

Are both #include <new> and using std::cout valid? Was it smart on his end to do this?


Depends on how you define smart? I personally prefer not to using anything and type std:: on everything, but what he's doing is sure as hell much better than using namespace std;
As for the new thing, No idea, the link I sent you can probably answer your question =)
Last edited on
Firstly, what is the point of "new"? "new" is a reserved keyword. And new throws a bad_alloc exception by default. What more could he possibly be doing?

http://en.cppreference.com/w/cpp/header/new
While new is a keyword, the operator new implementations are actually defined in that header. They are just implicitly included even if you do not explicitly include the header.
(See some notes in http://en.cppreference.com/w/cpp/memory/new/operator_new , for example. This is actually news to me, pun semi-intended.)
It seems more likely to me that your student was making use of an std::bad_alloc exception, which is also defined in that header.

Second, using std::cout? Is that even valid? It's funny because this particular student doesn't even appear to be using the iostream in the file.

It is absolutely valid, but is unnecessary if cout isn't actually used. It allows you to specifically pull in only cout from the std namespace rather than having to pull in everything with using namespace std; if you want to avoid have to type the std:: prefix.

Are both #include <new> and using std::cout valid?

You could test this by compiling the code, if you wanted.

Was it smart on his end to do this?

If your student was using something in each of those header files, then it would be required. Otherwise, it is at worst unnecessary.
Last edited on
> one of the students I'm grading
you should at least know how to search the documentation.

> Was it smart on his end to do this?
if it is a header, then using would be bad.
It would be good if they only include what they need, but extra headers don't hurt too much.

Can't say more with just 5 lines of code.
Last edited on
if it is a header,


It was a header. But thanks for everyone's comments.
Topic archived. No new replies allowed.