【发布时间】: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:所以……它会消失吗?有没有办法欺骗编译器?
-
它有问题,所以所有的赌注都取消了。就我个人而言,如果我知道警告是不正确的,我会压制它。
-
请注意,定义返回非
constbool或int结果的非const版本是没有意义的。 -
正如 ildjarn 所说,在处理引用或指针时,您只需要两个版本。例如,
template<class T> bool QuadTree<T>::IsEmpty() const { return _root == NULL; }就足够了。
标签: c++ visual-studio-2010 visual-c++ warnings