Avoiding a template template when calling the parent constructor

So, let's say I have a class with many template arguments, one of them is a derived class for using CRTP:

template <typename Derived, typename A, typename B, typename C, typename D>
class BaseFoo  {
public:
    BaseFoo(A& a) {}
};

And I want to inherit it:

class DerivedFoo : public BaseFoo<DerivedFoo, Bc, Cc, Dc, Ec> {
public:
    DerivedFoo(A& a) : BaseFoo<DerivedFoo, Bc, Cc, Dc, Ec>(a) {}
};

Is there any trick to avoid mentioning an explicit template argument?

This is normal if I still need to specify Derivedas template arguments.

+4
source share
2 answers

, . , , .

class DerivedFoo : public BaseFoo<DerivedFoo, Bc, Cc, Dc, Ec> {
public:
    DerivedFoo(A& a) : BaseFoo(a) {}
};

. , . , BaseFoo BaseFoo<...> .

+6

DerivedFoo.

class DerivedFoo;

typedef

typedef BaseFoo<DerivedFoo, Bc, Cc, Dc, Ec> OtherFoo;

using

using OtherFoo = BaseFoo<DerivedFoo, Bc, Cc, Dc, Ec>;

Derived

template <typename Derived>
using OtherFoo = BaseFoo<Derived, Bc, Cc, Dc, Ec>;

class DerivedFoo : public OtherFoo<DerivedFoo> {
    ....
};
+5

Source: https://habr.com/ru/post/1608227/


All Articles