如何使用和专门化'curious repeating template pattern'
How can I use and specialize a 'curious repeating template pattern'
我想将一些参数传递到一个奇怪的重复模板模式中。然后,我希望这个基类创建其他对象,并将这两种类型传递到子类中。这将允许我在调用子类之前对基类进行泛化,以执行一些公共逻辑。每个子类都应该能够成为一级层次结构的专门实例。
以下是如何做到这一点:
struct ParamOne {
double val {0.0};
};
struct ParamTwo {
int val {0};
};
template<typename P, typename Data, typename Other>
class Baseclass
{
public:
using subclass_type = P;
using data_type = Data;
using other_type = Other;
bool Method( const Data &data);
};
template<typename P, typename Data, typename Other> using pdata_type = typename P::data_type;
template<typename P, typename Data, typename Other> using pother_type = typename P::other_type;
template<typename P, typename Data, typename Other>
bool Baseclass<P, Data, Other>::Method( const Data &data )
{
P& Subclass = static_cast<P&>( *this );
pother_type<P, Data, Other> other;
other.val = 11;
return Subclass.SubclassMethod( data, other );
}
template<typename Data, typename Other>
class Subclass : public Baseclass<Subclass<Data, Other>, Data, Other>
{
public:
using data_type = Data;
using other_type = Other;
bool SubclassMethod( const Data &data, Other &other );
};
template<typename Data, typename Other>
bool Subclass<Data, Other>::SubclassMethod( const Data &data, Other &other )
{
return true;
}
template<>
bool Subclass<ParamOne, ParamTwo>::SubclassMethod( const ParamOne &data, ParamTwo &other )
{
return true;
}
int main(int argc, char **argv)
{
ParamOne one;
one.val = 5.0;
Subclass<ParamOne, ParamTwo> test;
test.Method(one);
return 0;
}
相关文章:
- 表示"accepting anything for this template argument" C++概念的通配符
- 传递给std::function template的template参数究竟代表什么
- 在template中使用std::variant的template函数
- C++ template for QList
- C++ - 为什么这里需要'template'关键字?
- 使用"std::enable_if_t" "function template has already been defined"
- C++模板错误:"invalid explicitly-specified argument for template parameter"
- C++ class template
- 在"template"和函数声明之间使用:template<typename trait> using tr = base_trait<trait> void fn(tr::t
- 为什么编译器说"candidate template ignored: couldn't infer template argument 'InputIterator'"?
- 为什么在这种情况下我需要 .template
- std::span constructor, libcxx vs libstdc++, template vs non-
- 模板函数参数到模板函数的"candidate template ignored: could not match ..."
- MacOS 上的 LLVM - 标准文件 iosfwd 中未知类型名称'template'
- std::get like (partial) template specialization
- 实现多类型算术运算符时如何解决"template argument deduction/substitution failure"
- 更新 Visual Studio 2017,现在出现编译错误 C7510:"回调":使用依赖模板名称必须以 'template' 为前缀
- C++ 中的"template <typename From, typename Tag> struct Alias;"是什么?
- Template类定义中的Template方法与声明不匹配
- 如何使用和专门化'curious repeating template pattern'