【问题标题】: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&lt;T&gt; 是在其他上下文中用于声明的类型。您必须使用模板参数编写整个类名。短格式 MyClass 只允许在定义中使用。

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

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

      这正是 ForEveRs 的答案。

      【讨论】:

        猜你喜欢
        • 2019-11-09
        • 2017-01-13
        • 2016-02-17
        • 2015-04-10
        • 2022-01-05
        • 1970-01-01
        • 2016-08-17
        • 2013-02-18
        • 2013-10-07
        相关资源
        最近更新 更多