[英]Doxygen complains about recursive C++ class
我有一個簡單的遞歸模板,實現了Euclid算法的優化版本。 Doxygen抱怨它:
/usr/home/kamikaze/stark/Yggdrasil/src/units/Units.hpp:335: warning: Detected potential recursive class relation between class units::euclid and base class units::euclid< Rhs, Lhs%Rhs >!
/usr/home/kamikaze/stark/Yggdrasil/src/units/Units.hpp:335: warning: Detected potential recursive class relation between class units::euclid and base class euclid< Rhs, Lhs%Rhs >!
/usr/home/kamikaze/stark/Yggdrasil/src/units/Units.hpp:335: warning: Detected potential recursive class relation between class units::euclid and base class units::euclid< Rhs, Lhs%Rhs >!
/usr/home/kamikaze/stark/Yggdrasil/src/units/Units.hpp:335: warning: Detected potential recursive class relation between class units::euclid and base class euclid< Rhs, Lhs%Rhs >!
我很傻眼為什么這是一個投訴/警告。 我認為遞歸類型是常見的和合法的。 它也是許多遞歸模板中的一個,但是唯一一個doxygen抱怨的。 令我驚訝的是,我只發現了類似的問題,其中doxygen錯誤地檢測了遞歸。
如果您有興趣,請輸入以下代碼:
/**
* Implements Euclid's algorithm to find the GCD between two integers.
*
* @tparam Lhs,Rhs
* The values to get the GCD for
*/
template <int Lhs, int Rhs>
struct euclid : euclid<Rhs, Lhs % Rhs> {
};
/**
* Terminates Euclid's algorithm.
*
* @tparam Gcd
* The GCD to return
* @see euclid
*/
template <int Gcd>
struct euclid<Gcd, 0> {
/**
* The GCD of the two original values.
*/
static constexpr int const value{Gcd};
};
這個結構確實超出了doxygen的解析能力。
由於這個類的用戶不知道它是以遞歸方式實現的,因此您可以使用以下解決方法:
/**
* Implements Euclid's algorithm to find the GCD between two integers.
*
* @tparam Lhs,Rhs
* The values to get the GCD for
*/
template <int Lhs, int Rhs>
struct euclid /** @cond */ : euclid<Rhs, Lhs % Rhs> /** @endcond */ {
/** @cond */
};
template <int Gcd>
struct euclid<Gcd, 0> {
/** @endcond
* The GCD of the two original values.
*/
static constexpr int const value {Gcd};
};
如果您事先申報模板會有所不同嗎? 在定義時,唯一可能的基類確實是遞歸的。 我會像這樣組織代碼,不僅僅是為了讓Doxygen高興,而且因為我還首先建立了與基本案例的數學歸納和遞歸關系:
// forward declaration
template <int Lhs, int Rhs>
struct euclid;
// base case
template <int Gcd>
struct euclid<Gcd, 0>
{
static constexpr int const value{Gcd};
};
// recurrence relation
template <int Lhs, int Rhs>
struct euclid : euclid<Rhs, Lhs % Rhs>
{
};
默認情況下,doxygen沒有完整的C ++解析器。 所以它可能會得到有效的C ++代碼錯誤。
由於doxygen 1.8.4可以配置 doxygen來使用Clang的C ++解析器,它應該解析大部分真實的C ++代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.