0

This is an annoying hole in my knowledge and I can't seem to find an answer.

I understand why it works during compilation (the header is never compiled in isolation, so stdafx.h will have already been included), but not why it works while editing.

Say I have the following files:

stdafx.h

#pragma once

#include <string>

MyFunction.h

#pragma once

void MyFunction( std::string Text );

MyFunction.cpp

#include "stdafx.h"
#include "MyFunction.h"

void MyFunction( std::string Text )
{
    // implementation here
}

When I'm editing MyFunction.h, how does Intellisense find std::string for the purpose of auto-complete, not showing it as undefined, etc?

I ask partly out of curiosity and partly because if I don't know the mechanism then I can't know if I'm at risk of breaking it or how to fix it when I do. I've seen it break a number of times, causing most of the header in question to be marked as an error, and then at some point it either just fixes itself or I accept it isn't coming back and add a few extra strategic includes into the header.

Just to repeat: I'm not asking how std::string is found during compilation, I'm asking how it's found when editing the header file.

Kemp
  • 3,467
  • 1
  • 18
  • 27
  • It won't necessary going to be able to find `std::string` when editing `MyFunction.h` because this header is not self contained. Even if `MyFunction.cpp` is in the same project. – user7860670 Nov 04 '19 at 14:28
  • That's exactly what I'd expect, and yet it does 99% of the time (for much more complex situations than this). Most non-trivial (and some trivial) examples of how to use precompiled headers are similar in that there are definitions that shouldn't be visible from the header, and yet I can find little evidence of anyone complaining about VS showing it as an error. I'm puzzled. – Kemp Nov 04 '19 at 14:36
  • Not sure how you manage to avoid an error here. For me intellisense not only shows an error, but also offers an include quickfix. Anyway, header files should be kept self-contained regardless of intellisense behavior. – user7860670 Nov 04 '19 at 14:39
  • My best guess is that it knows the .h and .cpp are a pair (as per the "Toggle Header / Code File" menu option) and knows that precompiled headers are in use, so it fetches definitions from stdafx.h because it knows that they'll be available at compilation time. – Kemp Nov 04 '19 at 14:39
  • Watch the bottom line - it tells you when intellisense is updating its database. – cup Nov 04 '19 at 14:39
  • I can reproduce same issue in my machine, but I can't make sure if this is the expected behavior. For `I understand why it works during compilation `: In VS, Intellisense should work with msbuild, so if the compile and build succeeds, the Intellisense has no reason to fail... – LoLance Nov 06 '19 at 09:59
  • 1
    The difference I see is that the build will be building a whole compilation unit, so by the time it pulls in the header it already has all the definitions from stdafx. If you're editing a header it doesn't have that knowledge because it doesn't include stdafx. I think I'm going to have to settle on the assumption that if precompiled headers are turned on then it assumes the definitions will always be available so it lets intellisense find them as a convenience even if not explicitly included in a header (as they shouldn't be). – Kemp Nov 06 '19 at 16:14
  • Hi Kemp, any update for this issue? If it still blocks you, feel free to let me know :) – LoLance Nov 14 '19 at 02:47

1 Answers1

1

How does VS Intellisense find definitions from stdafx.h while editing a header file?

After some tests in different versions of VS(VS2012~VS2019), I think what you experienced is the expected behavior about VS intellisense.

To answer your original question How does VS Intellisense find...:

When editing in MyFunction.h, I think the Intellisense find the definitions in stdafx.h by reading the data or info(#include statements) in MyFunction.cpp file.

That is to say: When I have a.h, b.h and b.cpp in current project, if b.cpp has statements #include "a.h" and #include "b.h", then Intellisense search the definitions in these three files. So even your move the #include <string> from stdafx.h to MyFunction.cpp file, the MyFunction.h can also recognize the definitions.

So if one test.h is referenced(included) by one test.cpp file, and this test.cpp file also use #include to reference(include) test2.h, test3.h... When editing test.h, Intellisense will automatically search definitions in test.cpp, test2.h, test3.h... In my opinion, that's how VS Intellisense works in this scenario. (Same behavior in VS2012, VS2015, VS2017, VS2019 in my machine)

Hope it helps to resolve your puzzle and if I misunderstand anything, please feel free to correct me :)

LoLance
  • 25,666
  • 1
  • 39
  • 73
  • This sounds reasonable and roughly matches what I was guessing was the answer, so I'll accept this one. I'm still not sure why it randomly breaks sometimes, but I suspect that'd need some insider knowledge. – Kemp Nov 15 '19 at 10:17