0

How can one derive a template class with templated type from boost::enable_shared_from_this?

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

This didn't compile:

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

Error: 'Myclass' is not a template type.

4

2 回答 2

1

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

template<template<class> class Container>
class Myclass : public boost::enable_shared_from_this<Myclass<Container> >
{
};
于 2013-07-12T11:38:08.007 回答
1

Normaly你使用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 的答案。

于 2013-07-12T12:16:36.867 回答