草庐IT

c++ - 这是 "new auto(enum_type)"的 Microsoft VC++ 2010 编译器错误吗

coder 2024-02-05 原文

环境:Microsoft Visual Studio 2010 with SP1 Preminum(10.0.40219.1 SP1Rel), Windows XP SP3

VC10 编译器支持 auto 关键字,但推导的类型相关信息对于枚举似乎并不总是正确的。

例子:

#include <type_traits>

enum fruit_t
{
    apple = 100,
    banana = 200,
};

int main()
{
    const auto pa = new auto(banana);
    const auto pb = new fruit_t(banana);
    static_assert(std::is_same<decltype(pa), decltype(pb)>::value, "not same!");
    delete pb;
    delete pa;
}

上面的代码应该没有编译时错误或运行时错误。但令我惊讶的是,它编译正常,没有任何错误或警告,但运行不正确。调试器在退出主函数后告诉:

检测到堆损坏:在 0x00034878 处的 %hs block (#55) 之后。 CRT 检测到应用程序在堆缓冲区结束后写入内存。

所以我猜测编译器可能在“自动”类型推导方面存在错误。下面的汇编程序窗口显示,在第一个“operator new”调用中请求的内存大小为 1 个字节,而第二个“operator new”调用为 4 个字节。这表明编译器在推导类型的大小上犯了一个大错误。

您认为这是编译器错误吗? Microsoft 是否修复了任何错误?

int main()
{
004113C0  push        ebp  
004113C1  mov         ebp,esp  
004113C3  sub         esp,10Ch  
004113C9  push        ebx  
004113CA  push        esi  
004113CB  push        edi  
004113CC  lea         edi,[ebp-10Ch]  
004113D2  mov         ecx,43h  
004113D7  mov         eax,0CCCCCCCCh  
004113DC  rep stos    dword ptr es:[edi]  
    const auto pa = new auto(banana);
004113DE  push        1  
004113E0  call        operator new (411181h)  
004113E5  add         esp,4  
004113E8  mov         dword ptr [ebp-104h],eax  
004113EE  cmp         dword ptr [ebp-104h],0  
004113F5  je          main+51h (411411h)  
004113F7  mov         eax,dword ptr [ebp-104h]  
004113FD  mov         dword ptr [eax],0C8h  
00411403  mov         ecx,dword ptr [ebp-104h]  
00411409  mov         dword ptr [ebp-10Ch],ecx  
0041140F  jmp         main+5Bh (41141Bh)  
00411411  mov         dword ptr [ebp-10Ch],0  
0041141B  mov         edx,dword ptr [ebp-10Ch]  
00411421  mov         dword ptr [pa],edx  
    const auto pb = new fruit_t(banana);
00411424  push        4  
00411426  call        operator new (411181h)  
0041142B  add         esp,4  
0041142E  mov         dword ptr [ebp-0F8h],eax  
00411434  cmp         dword ptr [ebp-0F8h],0  
0041143B  je          main+97h (411457h)  
0041143D  mov         eax,dword ptr [ebp-0F8h]  
00411443  mov         dword ptr [eax],0C8h  
00411449  mov         ecx,dword ptr [ebp-0F8h]  
0041144F  mov         dword ptr [ebp-10Ch],ecx  
00411455  jmp         main+0A1h (411461h)  
00411457  mov         dword ptr [ebp-10Ch],0  
00411461  mov         edx,dword ptr [ebp-10Ch]  
00411467  mov         dword ptr [pb],edx  
    static_assert(std::is_same<decltype(pa), decltype(pb)>::value, "not same!");
    delete pb;
0041146A  mov         eax,dword ptr [pb]  
0041146D  mov         dword ptr [ebp-0ECh],eax  
00411473  mov         ecx,dword ptr [ebp-0ECh]  
00411479  push        ecx  
0041147A  call        operator delete (411087h)  
0041147F  add         esp,4  
    delete pa;
00411482  mov         eax,dword ptr [pa]  
00411485  mov         dword ptr [ebp-0E0h],eax  
0041148B  mov         ecx,dword ptr [ebp-0E0h]  
00411491  push        ecx  
00411492  call        operator delete (411087h)  
00411497  add         esp,4  
}

最佳答案

是的,我认为这是一个 VS2010 错误。使用 XP SP3(32 位)和 VS2010 SP1 运行与您相同(或至少非常相似),我得到完全相同的错误。它看起来特定于枚举,因为在类中尝试显示一切正常。我还尝试将另一个水果项目添加到枚举中,其值为 100000,只是为了确保它不像您的枚举那样愚蠢,所有值都低于 255。结果相同。

我在 Microsoft Connect 上进行了快速搜索,而且我没有看到关于此的错误报告,因此我建议您输入一个。这是确保 Microsoft 知道并可能修复它的最佳方法。

关于c++ - 这是 "new auto(enum_type)"的 Microsoft VC++ 2010 编译器错误吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11260638/

有关c++ - 这是 "new auto(enum_type)"的 Microsoft VC++ 2010 编译器错误吗的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  4. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  5. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  6. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  7. ruby-on-rails - 相关表上的范围为 "WHERE ... LIKE" - 2

    我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que

  8. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  9. ruby - 安装 Ruby 时遇到问题(无法下载资源 "readline--patch") - 2

    当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub

  10. arrays - 这是 Ruby 中 Array.fill 方法的错误吗? - 2

    这个问题在这里已经有了答案:Arraysmisbehaving(1个回答)关闭6年前。是否应该这样,即我误解了,还是错误?a=Array.new(3,Array.new(3))a[1].fill('g')=>[["g","g","g"],["g","g","g"],["g","g","g"]]它不应该导致:=>[[nil,nil,nil],["g","g","g"],[nil,nil,nil]]

随机推荐