首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法调用构造函数(模板-模板参数)

无法调用构造函数(模板-模板参数)
EN

Stack Overflow用户
提问于 2017-02-08 21:39:46
回答 1查看 209关注 0票数 3

不知何故,我无法创建D的实例。我也不知道原因。模板-模板参数(Allocator_class)似乎是问题所在。

代码语言:javascript
复制
#include <vector>

template <template<typename T> class Allocator_class>
class T_b {
public:
    template<typename T> using A = Allocator_class<T>;
};

template< template <typename T> class A>
class C {
public:
    C() { }
};

template<typename Ttb>
class D {
public:
    template<typename T> using A = typename Ttb::template A<T>;
    typedef C<A> Data;

    D(C<A> &data) : _data(data) {}
private:
    Data &_data;
};

int main() {
    typedef T_b<std::allocator> Ttb;    
    C<std::allocator> b;
    D<Ttb>c(b);
}

来自Clang的错误:

代码语言:javascript
复制
test5.cpp:29:8: error: no matching constructor for initialization of 'D<Ttb>'
      (aka 'D<T_b<std::allocator> >')
        D<Ttb>c(b);
              ^ ~
test5.cpp:16:7: note: candidate constructor (the implicit copy constructor) not viable: no known
      conversion from 'C<std::allocator>' to 'const D<T_b<std::allocator> >' for 1st argument
class D {
      ^
test5.cpp:16:7: note: candidate constructor (the implicit move constructor) not viable: no known
      conversion from 'C<std::allocator>' to 'D<T_b<std::allocator> >' for 1st argument
class D {
      ^
test5.cpp:21:5: note: candidate constructor not viable: no known conversion from 'C<std::allocator>'
      to 'C<A> &' for 1st argument
    D(C<A> &data) : _data(data) {}
    ^
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-08 22:16:14

我无法解释为什么您的代码会出现错误。

但如果你想要一个解决办法..。使用类专门化和模板参数..。

代码语言:javascript
复制
#include <vector>

template <template<typename T> class Allocator_class>
class T_b
 {
   public:
      template<typename T> using A = Allocator_class<T>;
 };

template <template <typename T> class A>
class C
 {
   public:
      C() { }
 };

template <typename>
class D;

template <template <template <typename> class> class X,
          template <typename> class A>
class D<X<A>>
 {
   public:
      using Data = C<A>;

      D (Data & data) : _data(data) {}

   private:
      Data & _data;
 };

int main()
 {
    typedef T_b<std::allocator> Ttb;    

    C<std::allocator> b;

    D<Ttb> c(b);
 }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42124140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档