How to make a template type more specific internally to help content assist?
template<class T>class B{ //note: in real case, it has more template parameter
public: void f(){}
};
template<class B1>class C{ //<-- I know for sure that B1 derived from "B<something>".
B1* b;
void test(){
b->
^ ctrl+space doesn't show f()
}
};
My poor workaround is to create template specialization at class C
, but it will confuse content-assist in another way.
Below is another workaround but it is quite tedious.
I have to reflect the template argument at B
and use such reflect at C
one-by-one.
template<class T>class B{
public: using reflectiveT=T;
/* other T e.g. reflectiveT2=T2 , ... */
public: void f(){}
};
template<class B1>class C{
using BX=B<B1::reflectiveT>; //B<B1::reflectiveT1,..T2,...T3> ... tedious
BX* b;
void test(){
b->
^ ctrl+space will show f()
}
};
Issues:
- It suffer maintainabilty problem,
when I want to refactor
B
to have more/less template arguments later. - If
BX
is happen to be a class that derived fromB<something>
,BXT
will !=BX
.
I dream for something like :
template<class B1>class C{
using BX=B<...> as base of B1; //????
};
I may too rely on content-assist, but it greatly helps me to code very complex classes.
Edit
I can't just pass Args
inside B
as template parameter of C
, because C
may act wrong.
For example, B<D>::callback
will be call instead of D::callback
in the below code (demo):-
class x{};
template<class T>class B{
public: static void callback(){ std::cout<<"B<D>::callback()"; }
};
class D : public B<D>{ //
public: static void callback(){ std::cout<<"D::callback()"; }
};
template<class... Args>class C{
using BX=B<Args...>;
BX* b;
public: void test(){
BX::callback();
//^ will invoke B<D>::callback (wrong)
// instead of D::callback
}
};
int main(){
C<D> c; c.test(); //print "B<D>::callback()"
}
Edit: Simplify question a lot.