I want to omit some template parameter T1,T2
when create an instance of a class DeriveGenerator<T3,T4,T1,T2>
to comfort my life.
Here is the ultimately simplified version of what I am encountering.
My library:-
The important part is the class declaration. (this line
)
Their internal content is just a filler.
template<class T1,class T2>class BaseGenerator{ //<-- this line
public: std::pair<T1*,T2*> generateBase(){
/** actually create T3,T4 internally */
return std::pair<T1*,T2*>(nullptr,nullptr);
}
};
template<class T3,class T4,class T1,class T2>class DeriveGenerator{ //<-- this line
public: Base<T1,T2>* base;
public: std::pair<T3*,T4*> generateDerive(){
auto pp=base->generateBase();
return std::pair<T3*,T4*>((T3*)(pp.first),(T4*)(pp.second));
}
};
User:-
class B1{};class B2{};
class B3:public B1{};
class B4:public B2{};
int main() {
//v this is what I have to
BaseGenerator<B1,B2> baseGen;
DeriveGenerator<B3,B4,B1,B2> deriveGen; //so dirty #1
deriveGen.base=&baseGen;
deriveGen.generateDerive();
}
Question
Is it possible to make the line #1
cleaner?
I want the type of deriveGen
depends on the type of baseGen
.
Here is what I wish for :-
BaseGenerator<B1,B2> baseGen;
DeriveGenerator<B3,B4> deriveGen; //<-- modified
deriveGen.base=&baseGen;
or at least something like:-
BaseGenerator<B1,B2> baseGen;
DeriveGenerator<B3,B4, DECLARATION_TYPE(baseGen) > deriveGen; //<-- modified
deriveGen.base=&baseGen;
I have read (still no clue):-
- Omitting arguments in C++ Templates
- Skipping a C++ template parameter
- Difference when omitting the C++ template argument list
I don't even know if it is possible.
"decltype" seems to be the closest clue, but I can't find a way to apply it to this case.
I think I may have to split it to T1,T2
.... (?)
Edit
In real case, baseGen
is a non-static field of some classes that is not instantiated yet e.g.
class Holder{
public: BaseGenerator<B1,B2> baseGen;
};
Therefore, at the time of declaring deriveGen
, I can't reach the real instance of baseGen
.
That is the hard part.
I can refer baseGen
's type via decltype
, though.
(sorry for not mention about it)