问题
This is a fairly minor question, but it's annoying me: IntelliSense seems to be convinced that declaring static variables at the function-scope in an if
condition is an error, and complains about it. Only it builds just fine, and even the MSDN docs mention it as a legitimate usage. I'd really like to get rid of the wavy red line, because it comes up fairly often (it's used in a macro I use regularly).
Here's the code, as an example, though it's not the only example in my program:
MyForm::MyForm()
{
_VMESSAGE("Constructing '%s'/%p:%p @ <%p>",GetEditorID(),GetFormType(),formID,this);
if (static bool runonce = true)
{
// patch up vtbl
memaddr thisvtbl = (UInt32)memaddr::GetObjectVtbl(this);
_MESSAGE("Patching MyForm Form vtbl @ <%p>",thisvtbl);
gLog.Indent();
for (int i = 0; i < sizeof(Form_NoUseMethods)*0x8; i++)
{
if ((Form_NoUseMethods[i/0x20] >> (i%0x20)) & 1)
{
thisvtbl.SetVtblEntry(i*4,TESForm_vtbl.GetVtblEntry(i*4));
_VMESSAGE("Patched Offset 0x%04X",i*4);
}
}
gLog.Outdent();
runonce = false;
}
}
Both the static
in the if ( static bool runonce = true )
line and every usage of _MESSAGE
or _VMESSAGE
(which uses a similar construct) is underlined by IntelliSense, and hovering over any reads "Error: a storage class may not be specified here." Building the project produces no errors relating to these lines.
回答1:
The VC++ compiler allows this as a silent extension (it is not legal C++), but the VC++ IntelliSense engine is based on the EDG compiler frontend, not the VC++ compiler (go figure). So, the IntelliSense error is correct if you're concerned about writing portable code.
回答2:
Hold on -- are you saying that the following code compiles and runs in MSVC?
int main() {
if (static bool runonce = true) return 0 ;
}
That's a new one on me. g++ certainly doesn't accept it.
BTW, in your question, you mention "declaring static variables at the function-scope", which is not a problem:
int main() {
static bool runonce = true ;
if (runonce) return 0 ;
}
来源:https://stackoverflow.com/questions/6193506/vs-2010-c-intellisense-a-storage-class-may-not-be-specified-here-even-though