简体   繁体   English

当operator <在所有路径上递归时,Visual Studio 2008运行时堆栈溢出警告

[英]Visual Studio 2008 run-time stack overflow warning when operator< recursive on all paths

The C++ code below generates the following warning in Visual Studio 2008: 下面的C ++代码在Visual Studio 2008中生成以下警告:

1>c:...\\sample.cpp(6) : warning C4717: 'operator<' : recursive on all control paths, function will > cause runtime stack overflow 1> c:... \\ sample.cpp(6):警告C4717:'operator <':在所有控制路径上递归,函数将>导致运行时堆栈溢出

If I use the Sample class on any situation that the needs operator<, it actually crashes with a stack overflow error (for example after inserting the second Sample object in a multiset). 如果我在需要运算符<的任何情况下使用Sample类,它实际上会因堆栈溢出错误而崩溃(例如在多个集合中插入第二个Sample对象之后)。 The constructor keeps being called until it runs out of stack space. 构造函数一直被调用,直到它用完堆栈空间。

The code below is all that's needed to generate the warning on its own (without anything in the code referencing the Sample class). 下面的代码是自己生成警告所需的全部内容(代码中没有引用Sample类的任何内容)。

// Sample.hpp
#include <iostream>

class __declspec(dllexport) Sample
{
  std::string ID;
public:
  Sample (std::string id):ID(id) {};
  friend bool __declspec(dllexport) operator<(const Sample& s1, const Sample& s2);
};


// Sample.cpp
#include "Sample.hpp"

bool operator<(const Sample& s1, const Sample& s2)
{
  return s1.ID<s2.ID;
}

The warning shows with VC++ and VS2008 (Win32, /W3) on Win7. 在Win7上使用VC ++和VS2008(Win32,/ W3)显示警告。 For the same platform and exactly the same code, but with MinGW GCC 4.7.3 on eclipse, I don't get any warning. 对于相同的平台和完全相同的代码,但在eclipse上使用MinGW GCC 4.7.3,我没有得到任何警告。

If I add the < string > header the warning disappears in VS2008 and any use of the Sample class works perfectly fine. 如果我添加<string>标题,警告将在VS2008中消失,并且对Sample类的任何使用都可以正常工作。

Also, if I declare the Sample constructor explicit, VS2008 throws the following compile error: 另外,如果我声明Sample构造函数是显式的,VS2008会抛出以下编译错误:

1>.\\Sample.cpp(5) : error C2678: binary '<' : no operator found which takes a left-hand operand of > type 'const std::string' (or there is no acceptable conversion) 1> c:...\\Sample.hpp(13): could be 'bool operator <(const Sample &,const Sample &)' 1> while trying to match the argument list '(const std::string, const std::string)' 1>。\\ Sample.cpp(5):错误C2678:二进制'<':找不到运算符,它接受>类型'const std :: string'的左手操作数(或者没有可接受的转换)1> c :... \\ Sample.hpp(13):尝试匹配参数列表时可以是'bool operator <(const Sample&,const Sample&)'1>(const std :: string,const std :: string )”

However, setting the constructor explicit in GCC still doesn't generate any warnings nor errors (I set the warnings in Eclipse to the most comprehensive levels I could). 但是,在GCC中显式设置构造函数仍然不会生成任何警告或错误(我将Eclipse中的警告设置为最全面的级别)。

I'd like to know if someone can please roughly explain how VS2008 determines when to generate this stack overflow warning. 我想知道是否有人能够大致解释VS2008如何确定何时生成此堆栈溢出警告。 In this case, it turns out to be correct so I'm curious to see how it's done. 在这种情况下,它证明是正确的,所以我很想知道它是如何完成的。 Also, if possible, why GCC behaves differently here please. 另外,如果可能的话,为什么GCC在这方面表现不同。 Hopefully this makes sense. 希望这是有道理的。

This is happening because at the point of comparison std::string is an incomplete type and the conversion constructor Sample (std::string id) is being implicitly invoked. 发生这种情况是因为在比较时std::string是一个不完整的类型,并且隐式调用了转换构造函数Sample (std::string id) In the expression s1.ID<s2.ID both the LHS and RHS are being implicitly converted to a temporary Sample and the conversion operator then gets called again (and again and again). 在表达式s1.ID<s2.ID ,LHS和RHS都被隐式转换为临时Sample ,然后再次调用转换运算符(并且一次又一次)。

You need to include <string> so the complete definition of std::string is visible and I highly recommend declaring the constructor of sample explicit . 你需要包含<string>所以std::string的完整定义是可见的我强烈建议声明样本的构造函数explicit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Visual Studio 2008中关闭运行时检查 - turning off run-time check in visual studio 2008 为什么编译器报告“operator&lt;&lt; and operator&gt;&gt; recursive on all control path will cause stack overflow”? - Why does the compiler report "operator<< and operator>> recursive on all control paths will cause stack overflow "? 复制和交换习语警告:在所有控制路径上递归,函数将导致运行时堆栈溢出 - Copy&Swap idiom warning : recursive on all control paths, function will cause runtime stack overflow “在所有控制路径上递归,函数将导致运行时堆栈溢出”,重载&lt;&lt;运算符 - “recursive on all control paths, function will cause runtime stack overflow” on overloaded << operator C ++堆栈溢出-Visual Studio 2008 - C++ stack overflow - visual studio 2008 堆栈溢出后,Visual Studio 2008显示完整的堆栈跟踪 - Visual Studio 2008 show FULL stack trace after stack overflow Visual Studio C ++链接器警告:带有C运行时(CRT)的LNK4006 - Visual Studio C++ linker warning: LNK4006 with C Run-Time (CRT) Visual Studio运行时检查失败#2-围绕变量&#39;temp&#39;的堆栈已损坏 - Visual studio Run-Time Check Failure #2 - Stack around the variable 'temp' was corrupted 运行时检查失败#2-S,Visual Studio C ++ - Run-Time Check Failure #2 - S, Visual Studio C++ 在 Visual Studio 中关闭运行时检查 - Switch off Run-Time check in Visual Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM