2

All,

I have a strange issue with Intellisense for C++ projects. Background: I have recently installed VS2010 Sp1 and had a loss of Intellisense with SQL Server 2008 R2. I tried installing Cumulative Update 7 (CU7) as recommended and this did not work. Reinstalling SQL Server 2008 R2 however, did. I then updated using CU7 for SQL Server 2008 R2 and all was good. Now Intellisense works fine for C# projects but now for C++ projects it is not quite working as it should (NOTE: C++ project is not C++/CLI).

I have disabled Precompiled Header ("Not Using Precompiled Headers") and have the following includes:

#include <cmath>
#include <complex>
#include <iostream>
#include "stdafx.h"
#include "globals.h"

using namespace std;

NOTE: I am aware that using the std namespace is not always considered best practise, but here it will save me allot of time and there is no chance of ambiguity.

So now when I want a printf statement for example, if I type printf I get not Intellisense prompt. However, if I Ctrl+Space or Ctrl+J Intellisense is recognising the command, and it shows up. Below is the image of Intellisense not automatically working.

enter image description here

Now if I type std::printf, Intellisense appears:

enter image description here

Is there any way to let VS2010/Intellisense know I want to use the std namespace because it appears like it is just not recognising the "#using namespace std;" command.

Thanks very much.

MoonKnight
  • 23,214
  • 40
  • 145
  • 277
  • 1
    If you have disabled precompiled headers, you might as well remove the `#include "stdafx.h"`. – Sjoerd Aug 01 '11 at 11:45
  • 2
    To be clear: you want intellisense to pop up as soon as you start typing "p", and not at some recognized token (ie. after getting as far as "printf(")? If so, I don't think there is any way to do so -- ctrl+space is as close as you'll get. – Cory Nelson Aug 01 '11 at 11:58
  • Don't remember where i read it, but VC++ Intellisense works MUCH better with precompiled header in VS2010. There are some borderline cases even, where some features only work with precompiled headers. I saw this when i was trying to figure out why my VS 2010 was so slow compared to VS 2008. Why have you disabled pch? – RedX Aug 01 '11 at 12:09
  • When using Precompiled headers the code will not compile. It complains about the #include and all the other include statements for that matter. It also complains about using namespace std;. When I remove the pch then it compiles fine as I would expect. @Sjoerd If I remove #include "stdafx.h" having removed pchs, again I get a compile-time error. Yes I want it to pop up like with C#. It does pop up when I use std::printf() but not without the "std::" prefix - I think it should be possible. – MoonKnight Aug 01 '11 at 12:57
  • 2
    @Camus When using precompiled headers, your `#include "stdafx.h"` must be the first #include in the file, which explains why your compiler complains about the #include lines above the `#include stdafx.h`. – Sjoerd Aug 01 '11 at 13:25
  • @Cory_Nelson I think you are spot on - why don't you turn your comment into an answer? – Sjoerd Aug 01 '11 at 13:36
  • @Sjoerd The compile option /Yu'stdafx.h' is unchecked/not used so I don't think the order should matter. Ofcourse it would if I had the pchs turned on. My problem remains; even with pchs turned on and reordering the headers. Cory might be right, but I find it very odd that Intellisense for std::SomeFunction works and SomeFunction does not, when using namespace std; is clearly recongnised by the compiler - it may just be a limitation to Intellisense as Cory says? – MoonKnight Aug 01 '11 at 14:35

3 Answers3

2

Judging on your list of #include, printf() hasn't been declared. After all, it's declared in <stdio.h>/<cstdio>.

So Intellisense is right not to show it. ;)

Adding an #include <stdio.h> to the top has the advantage that no using namespace std; is required, as <stdio.h> puts the names in the global namespace (and possibly in the std:: namespace as well).

UPDATE: This does not explain why "std::printf" shows up after typing "std::pri".

UPDATE2: After testing, it is clear that printf will be suggested when forced by ctrl-space. Maybe the question is why Intellisense does not automatically pop up?

Sjoerd
  • 6,837
  • 31
  • 44
  • 1
    It is not unlikely that indirectly includes . – Bo Persson Aug 01 '11 at 12:01
  • It's c++ so he should use and use the proper namespace. @Bo It might be, but since he uses the function directly he should still include it himself. – RedX Aug 01 '11 at 12:07
  • @RedX - All true. I was aiming at the "This does not explain..." part of the answer. – Bo Persson Aug 01 '11 at 12:16
  • @RedX In C++, is also allowed. See the discussion at http://stackoverflow.com/questions/5079325/should-i-include-stddef-h-or-cstddef/5079548#5079548 – Sjoerd Aug 01 '11 at 12:25
  • @Bo Yes, C++ standard headers are allowed to include other C++ standard headers. Maybe Intellisense does not scan the C++ headers, but uses a cached representation? Pure speculation of course. I don't know what is going on, so maybe I should have written a comment instead of an answer... – Sjoerd Aug 01 '11 at 12:35
  • @Sjoerd - I think the answer is ok. Intellisense must scan all of the actual headers, including the nested ones, to see all macros and stuff that affects the symbols. – Bo Persson Aug 01 '11 at 12:41
  • Guys. Cheers for the comments. @Sjoerd You're ofcourse right, I should include #include /. But this is not making any difference to whether it compiles or to the Intellisense issue (perhaps a runtime error would be thrown - as this is for a DLL this is not a test I can do at this point). Bo is right, I do belive that is indirectly including - it should be included specifically though. Reguardless, the issue remains and is not due to the fact I have not included the headers. Again, if I press Ctrl+Space I get the Intellisense options? Thanks very much. – MoonKnight Aug 01 '11 at 13:12
  • @Sjoerd R.e. UPDATE 2. Indeed! It seems odd that it can't pop up, offering the desired options when Intellisense is clearly working and recognises the keywords - it does when prefixed with std:: not without. Bug?? It is doing the work but something is preventing the auto-options. Thanks again. – MoonKnight Aug 03 '11 at 09:09
2

Note: My second answer. Based on a comment by Cory Nelson, so I don't claim credit.

If I understand you correctly, you want that Intellisense pops up the moment you type 'p'.

The answer is: No, VS2010 won't do that.

Intellisense will kick in for "::p" or "std::p", but not for a plain "p". This has nothing to do with precompiled headers, nor #include order. It just won't.

Some of the IDEs I've worked with in the past have a setting to enable/disable completion in this case (I recall seeing so in some C++ Builder version), but I couldn't find it in VS2010.

So why would VS2010 not allow it? Keywords could cause problems that are not worth solving, but whether that's the reason here? I don't know. Maybe a member of the VS team could shed some light on this.

Update:

I tested this program in a new VS2010 project:

#include <string>
using namespace std;

int main()
{
    stri
}

No popup while typing "stri".

"string" appears in the pop-up after ctrl-space, and is absent when I remove the using namespace std; so it correctly reacts to the using.

After typing ::, Intellisense activates and after ::s suggests SA_AccessType (from the file sourceannotations.h), so the list is clearly prepopulated with many system headers.

If I type if and force Intellisense to pop-up, there is no if in the list. If I then type the ( as if I wanted to type if(, it corrects to ifstream(. So if Intellisense would always pop-up, it would be much harder to type keywords.

Sjoerd
  • 6,837
  • 31
  • 44
  • Brilliant. Thanks very much. Long and the short is, stop asking for too much and get on with it! Thanks again... – MoonKnight Aug 02 '11 at 09:39
1

That type of C++ intellisense only appears after you hit ctrl+space.

The commercial extension Visual Assist X gives an experience that may be more what you are looking for (note that it is not supported in express editions of Visual Studio).

sean e
  • 11,792
  • 3
  • 44
  • 56