【问题标题】:Is this a VS2012 optimization bug?这是 VS2012 的优化错误吗?
【发布时间】:2013-06-30 02:03:56
【问题描述】:

在这里完成 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 );
  }
}

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

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

edit 这应该排除 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;
  }
}

输出为not good

【问题讨论】:

  • 所以你说这适用于调试版本并在发布版本中显示不同的行为?只有这个片段,没有别的?
  • http://stackoverflow.com/a/5108118/1807078 可能是相关的。确保使用 VS2012 编译 dll 和 unittest++。
  • @BalogPal 确实,debug 很好,release 不行。不确定“仅此片段”是什么意思;项目中有数百个测试,但这个失败了。
  • @ChrisCooper 一切都使用 VS212 编译并使用相同的设置。另请参阅编辑。
  • @stijn:我的意思是隔离良好。发布的代码看起来不错。但是,如果某些不相关的部分导致未定义的行为,或者您设法破坏 ODR,这可能会在使用好的编译器观察到的不当行为中体现出来。那么你能用这个最少的代码重现这个错误吗?

标签: c++ optimization visual-studio-2012 unittest++


【解决方案1】:

这是confirmed to be an optimizer bug,他们希望在 VS2012 更新 4 中修复它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    相关资源
    最近更新 更多