gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 22:57:59 25 4
gpt4 key购买 nike

我无法创建 D 的实例。我不知道为什么。模板模板参数 (Allocator_class) 似乎是问题所在。

#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 的错误:

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) {}
^

最佳答案

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

但是如果你想要一个解决方案......使用类特化和模板模板模板参数......

#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);
}

关于c++ - 无法调用构造函数(模板-模板参数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42124140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com