gpt4 book ai didi

c++ - 奇怪的重复模板 - 变体

转载 作者:可可西里 更新时间:2023-11-01 17:23:09 28 4
gpt4 key购买 nike

关于 CRP如果我想实现它的细微变化(使用模板模板参数),我会得到一个编译错误:

template <template <typename T> class Derived>
class Base
{
public:
void CallDerived()
{
Derived* pT = static_cast<Derived*> (this);
pT->Action(); // instantiation invocation error here
}
};

template<typename T>
class Derived: public Base<Derived>
{
public:
void Action()
{
}
};

我不太确定有人会选择这种形式(不适合我编译)而不是使用这种形式(这有效)

template <typename Derived>
class Base
{
public:
void CallDerived()
{
Derived* pT = static_cast<Derived*> (this);
pT->Action();
}
};

template<typename T>
class Derived: public Base<Derived<T>>
{
public:
void Action()
{
}
};

最佳答案

这也应该编译。我们只需要明确指定的其他模板参数

 template <typename T, template <typename T> class Derived>
class Base
{
public:
void CallDerived()
{
Derived<T>* pT = static_cast<Derived<T>*> (this);
pT->Action(); // instantiation invocation error here
}
};

template<typename T>
class Derived: public Base<T,Derived>
{
public:
void Action()
{
}
};

关于c++ - 奇怪的重复模板 - 变体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10374650/

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