【问题标题】:C++ Visual Studio 2010 C4717 compiler warning in one part of code but not in anotherC++ Visual Studio 2010 C4717 编译器在代码的一部分中发出警告,但在另一部分中没有
【发布时间】:2012-04-10 22:42:37
【问题描述】:

这六种方法(实际上是三种,实际引发警告的是非常量版本)正在导致 C4717(所有路径上的函数递归)警告,但这些方法之后的方法却没有(与我能做到的完全相同)告诉...)。 我错过了什么导致这些警告而不是另一个警告?

警告生成方法:

template<class T>
const QuadTreeNode<T>* QuadTree<T>::GetRoot() const {
    return _root;
}


template<class T>
QuadTreeNode<T>* QuadTree<T>::GetRoot() {
    return static_cast<const QuadTree<T> >(*this).GetRoot();
}

template<class T>
const int QuadTree<T>::GetNumLevels() const {
    return _levels;
}

template<class T>
int QuadTree<T>::GetNumLevels() {
    return static_cast<const QuadTree<T> >(*this).GetNumLevels();
}

template<class T>
const bool QuadTree<T>::IsEmpty() const {
    return _root == NULL;
}


template<class T>
bool QuadTree<T>::IsEmpty() {
    return static_cast<const QuadTree<T> >(*this).IsEmpty();
}

非警告生成方法:

template<class T>
const Rectangle QuadTreeNode<T>::GetNodeDimensions() const {
    return _node_bounds;
}

template<class T>
Rectangle QuadTreeNode<T>::GetNodeDimensions() {
    return static_cast<const QuadTreeNode<T> >(*this).GetNodeDimensions();
}

【问题讨论】:

  • 该警告是错误的并且报告误报。有关详细信息,请参阅this bug report
  • @ildjarn:所以……它会消失吗?有没有办法欺骗编译器?
  • 它有问题,所以所有的赌注都取消了。就我个人而言,如果我知道警告是不正确的,我会压制它。
  • 请注意,定义返回非const boolint 结果的非const 版本是没有意义的。
  • 正如 ildjarn 所说,在处理引用或指针时,您只需要两个版本。例如,template&lt;class T&gt; bool QuadTree&lt;T&gt;::IsEmpty() const { return _root == NULL; } 就足够了。

标签: c++ visual-studio-2010 visual-c++ warnings


【解决方案1】:

正如ildjarn 所述,这是一个带有警告的acknowledged 错误。如果您以与您的代码类似的最基本用法查看代码,则不会出现以下警告(并且不是递归的)。

class A
{
public:
    bool IsEmpty()
    {
        return static_cast<const A>(*this).IsEmpty();
    }

    bool IsEmpty() const
    {
        return true;
    }
};

int main()
{
    A whatever;
    whatever.IsEmpty();

    return 0;
}

【讨论】:

    猜你喜欢
    • 2011-01-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多