【问题标题】:How to derive a template template class from boost::enable_shared_from_this?如何从 boost::enable_shared_from_this 派生模板模板类?
【发布时间】:2013-07-12 11:32:09
【问题描述】:
如何从 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”不是模板类型。
【问题讨论】:
标签:
c++
boost
shared-ptr
template-templates
【解决方案1】:
由于您的类是由模板模板参数模板化的 - 您应该简单地使用Containter。
template<template<class> class Container>
class Myclass : public boost::enable_shared_from_this<Myclass<Container> >
{
};
【解决方案2】:
通常你使用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 的答案。