如何从boost::enable_shared_from_this派生模板模板类

How to derive a template template class from boost::enable_shared_from_this?

本文关键字:this 派生 from shared boost enable      更新时间:2023-10-16

如何从boost::enable_shared_from_this派生模板化类型的模板类?

template<template<class T> class Container>
class Myclass : public boost::enable_shared_from_this<?> {
};

这没有编译:

template<template<class T> class Container>
class Myclass : public boost::enable_shared_from_this<Myclass<Container<T> > > {
};

错误:"Myclass"不是模板类型。

由于您的类是由template-template参数模板化的,因此您应该简单地使用Containter

template<template<class> class Container>
class Myclass : public boost::enable_shared_from_this<Myclass<Container> >
{
};

通常以以下方式使用boost::enable_shared_from_this

class Myclass 
  : public boost::enable_shared_from_this<Myclass>
{
  // ...
};

如果你有一个模板,它会更改为

template<class T>
class Myclass 
  : public boost::enable_shared_from_this<Myclass<T> >
{
  // ...
};

其中Myclass<T>是在其他上下文中用于声明的类型。您必须使用模板参数编写整个类名。缩写形式MyClass仅允许在定义内部使用。

对于模板模板参数,您必须使用

template<template<class> class T>
class Myclass 
  : public boost::enable_shared_from_this<Myclass<T> >
{
  // ...
};

这是ForEveRs的答案。

相关文章: