C++ Visual Studio 2010 C4717 编译器警告在代码的一部分,但不在另一部分

C++ Visual Studio 2010 C4717 compiler warning in one part of code but not in another

本文关键字:一部分 Visual 另一部 代码 C4717 2010 编译器 警告 C++ Studio      更新时间:2023-10-16

这六个方法(实际上是三个,实际抛出警告的是非常量版本)导致 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();
}

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

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;
}