Reputation: 6390
Given the A.h
file below (note that on purpose, I didn't include the <iostream>
header in this file). The VS2010 text editor then shows a red line below std
, as it doesn't recognize this name.
class A
{
public;
A() throw(std::bad_alloc);
};
But if I include the <iostream>
header in the A.cpp
file as below, the red line disappears in the A.h
file, even when the A.cpp
is closed. How is this possible ?
#include <iostream>
#include "A.h"
A::A() { throw std::bad_alloc(); }
Upvotes: 1
Views: 204
Reputation: 474436
Visual Studio is written for all C++ programmers. This means that it cannot assume that header files always include everything needed by them. To put red lines under everything that isn't explicitly included by a header would mean a lot of false positives for those developers who work in a strict environment where headers are included in a specific order, with dependencies.
So Visual Studio tries to figure out what you mean, rather than assuming that your headers are able to stand on their own.
It may not be the best practice in your source, but they have to account for it.
Upvotes: 0
Reputation: 3579
Add a new C++ file that does include A.h
but doesn't include <iostream>
. The red underline under std
will reappear.
VS2010 knows which files include that header file. If any of them do not know about std
, it will point out the issue.
But you're right, if you switch the order of the includes so that the project won't compile, it still removes the red underline.
Upvotes: 1