VS 2010 C++ IntelliSense“此处不能指定存储类别”即使可以吗?

发布于 2024-11-10 18:05:59 字数 1162 浏览 3 评论 0原文

这是一个相当小的问题,但它让我很恼火:IntelliSense 似乎确信在 if 条件中声明静态变量 在函数范围 是一个错误,并抱怨关于它。只有它构建得很好,甚至 MSDN 文档也提到它是合法的用法。我真的很想去掉红色波浪线,因为它经常出现(它在我经常使用的宏中使用)。

这是代码,作为示例,尽管它不是我的程序中的唯一示例:

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;
    }
}

if ( static bool runonce = true ) 行中的 static 以及每个用法_MESSAGE_VMESSAGE(使用类似的构造)由 IntelliSense 加上下划线,并将鼠标悬停在任何内容上“错误:此处可能未指定存储类”。构建项目不会产生与这些行相关的错误。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海蓝天 2024-11-17 18:05:59

VC++ 编译器允许将此作为​​静默扩展(它不是合法的 C++),但 VC++ IntelliSense 引擎基于 EDG 编译器前端,而不是 VC++ 编译器(见图)。因此,如果您担心编写可移植代码,则 IntelliSense 错误是正确的。

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.

朦胧时间 2024-11-17 18:05:59

等等——您是说下面的代码在 MSVC 中编译并运行吗?

int main() {
  if (static bool runonce = true) return 0 ;
}

这对我来说是一种新的体验。 g++ 当然不接受它

顺便说一句,在您的问题中,您提到“在函数范围内声明静态变量”,这不是问题:

int main() {
  static bool runonce = true ;
  if (runonce) return 0 ;
}

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 ;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文