Why might the Q_FOREACH macro break VS2010 intelli

2020-04-05 08:16发布

I have a c++ project in VS2010 with Qt 4.7.4 and I frequently have problems with IntelliSense (as we all do...). A specific problem is that sometimes the function body (in the .cpp file) can't be found. If I click into that function body, the dropdown box (which usually shows the function you're in) is empty.

Today I noticed this behavior right after I added a foreach() statement (the Qt macro Q_FOREACH). I then replaced the foreach() macro with a corresponding for(int i = 0; i < ...) and IntelliSense immediately displayed the function again.

So I'd like to know:

  1. Can somebody please verify this behavior and tell me so in a comment?
  2. Why does this happen?
  3. Is there a workaround which lets me continue to use Q_FOREACH?

1条回答
等我变得足够好
2楼-- · 2020-04-05 08:54

You have to use the concept of 'cpp.hint' files.

Basically, you have to put the troublesome macros into a file named 'cpp.hint' and put that file in your solution directory (which did not work for me - non-standard project layout maybe) OR in a parent-directory where your code files reside in. (worked for me)

In that file you just put the troublesome macros WITHOUT right-hand-side, so in your case:

#define foreach()

or maybe better

#define Q_FOREACH(variable, container)
#define foreach(...)
etc.

NOTE, that you may have to ReScan or Restart or fiddle around with a function for the effect to set in after putting the definition in the cpp.hint file.

UPDATE: Indeed, I just found, that I have to make some change to a .cpp file (e.g. adding a new line) for the effect to kick in. The fix is not automatically applied.

The original link is: http://msdn.microsoft.com/en-us/library/dd997977.aspx

The reason for the trouble is that Intellisense performance would (potentially) decrease dramatically if it had to parse all macros in a project, so it only parses those given explicitly in 'cpp.hint'.

The original microsoft text says that you can use any directory in "The path from the root directory of a source file to the directory that contains the source file itself. In a typical Visual C++ project, the root directory contains the solution or project file."

You can find the main 'cpp.hint' file at 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcpackages' for reference

查看更多
登录 后发表回答