[英]How to cause template prototype to inherit so all specializations inherit the same class/interface?
[英]How to inherit same class multiple times?
是否可以像這樣繼承兩個相同的類? 我能夠在 msvc 上編譯它,但無法在 g++/clang++ 上編譯。 我收到錯誤:
Source.cpp:14:12: warning: direct base 'a' is inaccessible due to ambiguity:
struct b -> struct a
struct b -> class_combiner<struct a> -> struct a [-Winaccessible-base]
struct b : public a, public class_combiner<a> {
^~~~~~~~
Source.cpp:17:12: error: ambiguous conversion from derived class 'b' to base class 'a':
struct b -> struct a
struct b -> class_combiner<struct a> -> struct a
a::f();
資源:
template <typename T>
struct class_combiner : public T {
using T::T;
};
struct a {
void f() {}
};
struct b : public a, public class_combiner<a> {
void f2() {
a::f();
b::class_combiner::f();
}
};
int main() {
b x;
x.f2();
}
我想出了可行的快速修復:
template <typename T, unsigned int instance>
struct class_combiner : public T {
using T::T;
};
struct a {
void f() {}
};
struct b : public class_combiner<a, 0>, public class_combiner<a, 1> {
void f2() {
class_combiner<a, 0>::f();
class_combiner<a, 1>::f();
}
};
int main() {
b x;
x.f2();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.