【发布时间】:2009-06-05 05:39:15
【问题描述】:
我正在尝试使用 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>(来自 Barton 和 Nackman 的把戏)。使用 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,则一切编译正常。
有什么想法吗?
【问题讨论】: