加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 这是VS2012优化错误吗?

发布时间:2020-12-16 07:05:00 所属栏目:百科 来源:网络整理
导读:在这里完成VS2010到2012的更新,并且由于代码生成错误或编程错误而导致一些单元测试失败,但我不确定是哪一个. 我发布的代码几乎等于原始代码,并且还会重现问题. 这是一个案例;所有类/实现都在单独的文件中. class Base{public: virtual ~Base(){} void DoIt()
在这里完成VS2010到2012的更新,并且由于代码生成错误或编程错误而导致一些单元测试失败,但我不确定是哪一个.
我发布的代码几乎等于原始代码,并且还会重现问题.

这是一个案例;所有类/实现都在单独的文件中.

class Base
{
public:
  virtual ~Base(){}
  void DoIt(){ DoItImpl(); }
protected:
  virtual void DoItImpl() = 0;
};

class Base2 : public Base
{
public:
  virtual void DoStuff() = 0;
};

class Impl : public Base2
{
public:
  Impl();
  void DoStuff();
protected:
  void DoItImpl();
};

void Impl::DoStuff()
{
  throw std::runtime_error( "oops" );
}

void Impl::DoItImpl()
{
  throw std::runtime_error( "oops" );
}

以上内容位于一个DLL中,并在exe中使用unittest进行测试(为了清楚起见,我为CHECK_THROW宏进行了测试,虽然没有改变):

SUITE( Base )
{
  TEST( Impl )
  {
    Impl c;
    bool caught = false;
    try
    {
      c.DoIt();
    }
    catch( const std::exception& )
    {
      caught = true;
    }

    //this point is never reached,instead control goes to the
    //try/catch in unittest++'s ExecuteTest function
    CHECK( caught );
  }
}

无论是否是错误,是否有我可以立即使用的解决方法,或者有关如何避免这种情况的一般规则?

编辑添加DoStuff()的实现,如果我调用它而不是DoIt()没有问题!

编辑这应该排除unittest框架或任何其他代码是问题的可能性,或者通过const引用捕获,或者编译器不知道runtime_error来自异常;我扩展了unittest宏以显示它们实际执行的操作,并创建了一个仅包含此源文件的新项目:

namespace SuiteImpl
{
  class TestImpl
  {
  public:
    TestImpl() {}
    virtual void RunImpl() const;
  };

  void TestImpl::RunImpl() const
  {
    xxx::Impl c;
    try
    {
      c.DoIt();
    }
    catch( std::exception& )
    {
      std::cout << "good" << std::endl;
    }
  }
}

int main()
{
  SuiteImpl::TestImpl impl;

  try
  {
    impl.RunImpl();
  }
  catch( const std::exception& )
  {
    std::cout << "not good" << std::endl;
  }
}

输出不好.

解决方法

这是 confirmed to be an optimizer bug,他们希望修复它VS2012 Update 4.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读