【发布时间】:2022-01-03 06:25:05
【问题描述】:
我的理解是,在 C++ 中,用户定义的类型是被继承的,而且在某种程度上确实如此。
这是我正在尝试做的,但这不能编译:
template <typename T>
struct A{
using type = T;
};
template <typename T>
struct B: A<T>{
using type2 = type;
};
但是,如果我有 B 扩展 A 的非模板参数,它可以工作:
template <typename T>
struct A{
using type = T;
};
template <typename T>
struct B: A<int>{
using type2 = type;
};
确实,我发现了一种解决方法(但对于我正在尝试做的事情并没有真正帮助我的是范围 type 但这会破坏继承点:
template <typename T>
struct A{
using type = T;
};
template <typename T>
struct B{
using type2 = A<T>::type;
};
是否有适当的方法来实现我想要的行为(在更一般的情况下,这显然是一个玩具示例)以及它的语义是什么?
编辑:这是我希望代码的样子,可以解决缺乏继承的问题,但我想了解限制是什么
template <typename t>
struct instruction{
public:
using input = t::first;
using mem = t::second;
using newmem = mem;
using newinput = input;
using result = pair<newinput, newmem>;
};
template <typename t>
struct right_code: public instruction<t>{
using newmem = buffer<cons<mem::list2::head, mem::list1>, (std::is_same<mem::list2::tail , nil> ? cons<zero, nil>
: mem::list2::tail)>;
};
template <typename t>
using right = right_code<t>::result;
template <typename t>
struct left_code:instruction<t>{
using newmem = (std::is_same<mem::list1::tail, nil ? mem
: buffer<mem::list1::tail, cons<mem::list1::head, mem::list2>);
};
template <typename t>
using left = left_code<t>::result;
【问题讨论】:
-
你能举一个例子来说明为什么
using type2 = A<T>::type;不适合你吗?在这种涉及依赖于模板参数的基类的情况下,需要某种明确的限定;问题是你可以接受什么样的结构。 -
查看this 了解可能的重复。
-
我认为问题是为什么如果
B是A<T>,B::type不存在,就像A有int foo() const;,B会有它也是。
标签: c++ templates template-meta-programming