我试图访问继承结构中的类型,但是如果我将deruved类作为模板,这就变得不可能(?)。 在以下示例中,如何从结构派生
访问基本类型
(如果可能):
template <typename T>
struct Base {
struct BaseType {
T x;
};
void f(BaseType &p) { (void)p.x; }
};
template <typename TBase>
struct Derived : Base<TBase> {
BaseType value; // <-- COMPILE ERROR
Derived(TBase v) : value{v} {}
void f() { Base<TBase>::f(value); }
};
Derived<int> d{10};
int main(void) {
d.f();
return 0;
}
解决方案最好在C++14中。 先谢谢你。
template <typename T>
struct Base {
struct BaseType {
T x;
};
void f(BaseType &p) { (void)p.x; }
};
template <typename TBase>
struct Derived : Base<TBase> {
typename Base<TBase>::BaseType value; // <-- No COMPILE ERROR
Derived(typename Base<TBase>::BaseType v) : value{v} {}
void f() { Base<TBase>::f(value); }
};
Derived<int> d{Base<int>::BaseType{}};
int main(void) {
d.f();
return 0;
}