我正在尝试使用 Barton 和 Nackman 技巧来实现类以避免动态调度。(我正在编写性能很重要的 MCMC 代码。)我不是 C++ 专家,但基本技巧在其他地方对我有用。但是,我现在遇到了需要对第二个派生类进行模板化的情况。这似乎会引起问题。我的代码大纲是:
// Generic step class
template<class DerivedStepType>
class Step {
public:
DerivedStepType& as_derived() {
return static_cast<DerivedStepType&>(*this);
}
void DoStep() {
return as_derived.DoStep();
}
};
// Gibbs step
template<class DerivedParameterType> // THIS IS THE PROBLEM
class GibbsStep : public Step<GibbsStep> {
public:
GibbsStep(DerivedParameterType new_parameter) {
}
void DoStep() {
}
};
问题template<class DerivedParameterType>
如下<GibbsStep>
(来自巴顿和纳克曼的把戏)。使用 g++ v 4.01 (OSX),我收到以下错误:
./src/mcmc.h:247: error: type/value mismatch at argument 1
in template parameter list for 'template<class DerivedStepType> class Step'
./src/mcmc.h:247: error: expected a type, got 'GibbsStep'
如果删除template<class DerivedParameterType>
并替换DerivedParameterType
为double
.
有任何想法吗?