I have something like the following:
template <typename T>
class A {
public:
typedef T Type;
};
template <typename...> class B;
template <typename TFirst, typename... TRest>
class B<TFirst, TRest...> : public B<TRest...> {
public:
typedef typename TFirst::Type Type;
TFirst a;
virtual void func(TFirst a, Type t) = 0;
};
template <>
class B<> {};
struct X {};
struct Y {};
class ASub1 : public A<X> {};
class ASub2 : public A<Y> {};
class BSub : public B<ASub1, ASub2> {
public:
// Implement all pure virtual methods
// void func(ASub1 a1, Type t) override {}
// void func(ASub2 a2, Type t) override {}
};
Now I have two questions:
How would I be able to implement all pure virtual methods in BSub
? I somehow need to access the template arguments (types) of all ASubX
.
Is there a way to access all the members a
by passing the ASubX
? I mean something like get<ASub1>(b_sub)
if b_sub
were an instance of BSub
.
I would prefer a solution with C++14.
For (1), you can write
class BSub : public B<ASub1, ASub2> {
public:
void func (ASub1, typename ASub1::Type) override {}
void func (ASub2, typename ASub2::Type) override {}
};
or, remebering that ASub1::Type
is X
and ASub2::Type
is Y
,
class BSub : public B<ASub1, ASub2> {
public:
void func (ASub1, X) override {}
void func (ASub2, Y) override {}
};
For (2), as suggested by WF, you can use
b_sub.B<ASub1, ASub2>::a
to access the ASub1
component and
b_sub.B<ASub2>::a
to access the ASub2
one.
The following is a full working (simplified) example
#include <iostream>
#include <type_traits>
template <typename T>
struct A
{ using Type = T; };
template <typename...>
struct B;
template <typename TFirst, typename ... TRest>
struct B<TFirst, TRest...> : public B<TRest...>
{
using Type = typename TFirst::Type;
TFirst a;
virtual void func (TFirst a, Type t) = 0;
};
template <>
struct B<> {};
struct X {};
struct Y {};
struct ASub1 : public A<X> {};
struct ASub2 : public A<Y> {};
struct BSub : public B<ASub1, ASub2>
{
void func (ASub1, X) override { std::cout << 1 << std::endl; }
void func (ASub2, Y) override { std::cout << 2 << std::endl; }
};
int main()
{
BSub bs;
bs.func(ASub1{}, X{});
bs.func(ASub2{}, Y{});
using T1 = decltype(bs.B<ASub1, ASub2>::a);
using T2 = decltype(bs.B<ASub2>::a);
static_assert(std::is_same<T1, ASub1>{}, "!");
static_assert(std::is_same<T2, ASub2>{}, "!");
}
Inspired by an implementation of std::get
for std::tuple
I came up with following solution.
Thanks for your help.
#include <cstdio>
template <typename T>
class A {
public:
using Type = T;
};
template <typename...> class B;
template <typename TFirst, typename... TRest>
class B<TFirst, TRest...> : public B<TRest...> {
public:
using AType = typename TFirst::Type;
using Type = B<TFirst, TRest...>;
TFirst a;
virtual void func(TFirst a, AType t) {}
};
template <>
class B<> {};
struct X {};
struct Y {};
class ASub1 : public A<X> { public: int value = 10; };
class ASub2 : public A<Y> { public: int value = 20; };
class BSub : public B<ASub1, ASub2> {
public:
// Implement all pure virtual methods
void func(ASub1 a1, typename ASub1::Type t) {}
void func(ASub2 a2, typename ASub2::Type t) {}
};
template <typename TA, typename TB> class BElement;
template <typename TA, typename TAFirst, typename... TARest>
class BElement<TA, B<TAFirst, TARest...>> : public BElement<TA, B<TARest...>> {
public:
};
template <typename TA, typename... TARest>
class BElement<TA, B<TA, TARest...>> {
public:
using AType = TA;
using BType = B<TA, TARest...>;
};
template <typename TA, typename... TAs>
TA& get(B<TAs...> & b) {
using BType = typename BElement<TA, B<TAs...>>::BType;
return static_cast<BType &>(b).a;
}
template <typename TA, typename TB>
TA& get(TB b) {
return get<TA>(static_cast<typename TB::Type &>(b));
}
int main() {
BSub bsub;
printf("%d\n", get<ASub1>(bsub).value);
printf("%d\n", get<ASub2>(bsub).value);
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.