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:
B
to have more/less template arguments later. BX
is happen to be a class that derived from B<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.
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.
You can separate the implementation and the interface for each function:
#include <iostream>
// Base class with default implementations and common interface
template<class T> class B {
static void callback_impl() { std::cout<<"B<T>::callback()\n"; }
void f_impl() { std::cout << "B<T>::f()\n"; }
public:
static void callback() { T::callback_impl(); }
void f() { static_cast<T*>(this)->f_impl(); }
};
class D1 : public B<D1>{ // D1 overrides callback
friend class B<D1>;
static void callback_impl(){ std::cout<<"D1::callback()\n"; }
};
class D2 : public B<D2> { // D2 overrides f
friend class B<D2>;
void f_impl() { std::cout << "D2::f()\n"; }
};
template<class... Args>class C{
using BX=B<Args...>;
BX b;
public: void test(){
BX::callback();
b.f();
}
};
int main(){
C<D1> c1; c1.test(); //print "D1::callback()\nB<T>::f()\n"
C<D2> c2; c2.test(); //print "B<T>::callback()\nD2::f()\n"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With