gpt4 book ai didi

c++ - 模板化的 Barton 和 Nackman 技巧问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:26:07 26 4
gpt4 key购买 nike

我正在尝试使用 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 .

有什么想法吗?

最佳答案

GibbsStep 是一个模板类,在 public Step<GibbsStep> 中使用时需要实例化.您应该将其更改为 public Step<GibbsStep<DerivedParameterType> >

关于c++ - 模板化的 Barton 和 Nackman 技巧问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/954398/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com