r/cpp • u/Reflection_is_great • Mar 17 '26
Easy Virtual Template Function. C++26
Have you ever wanted to use virtual template functions but found existing methods have so much boilerplate? Ever thought that virtual template functions could be done without it? Well, with the new reflection features, it can be!
The main goal was to minimize the amount of code required to use virtual template functions. This has been accomplished. Each base class needs to inherit only one class, and each base virtual template function requires one line of code to provide the minimal information required. This looks a lot nicer as it is very similar to how normal virtual functions are created.
Simple example:
struct D1;
struct D2;
struct Base: VTF::enable_virtual_template_functions<D1,D2>{
template<typename T>
Base(T* ptr): enable_virtual_template_functions(ptr){}
template<typename T>
int f(int a){
constexpr auto default_function = [](Base* ptr, int a){ return 99;};
CALL_VIRTUAL_TEMPLATE_FUNCTION(^^T,default_function,a);
}
};
struct D1:Base{
D1():Base(this){}
template<typename T>
int f(int a){
return 11;
}
};
struct D2:Base{
D2():Base(this){}
template<typename T>
int f(int a){
return 12;
}
};
int main(){
using PtrT = std::unique_ptr<Base>;
PtrT a = std::make_unique<D1>();
PtrT b = std::make_unique<D2>();
assert((a->f<int>(1) == 11));
assert((b->f<int>(1) == 12));
}
View all comments
Show parent comment
1
u/Reflection_is_great Mar 18 '26
Sure! I’m very happy to share.
Basically, whenever you instantiate a function with the “CALL_VIRTUAL_TEMPLATE_FUNCTION” macro, at compile-time, an array of function pointers is generated, which serves as a vtable for that specific function. The function pointers will be the pointers to either the overriding functions in the derived classes or the default function if a derived class does not have an overriding function.
So, in the example, when we call Base::f<int>(int), an array of two function pointers is created. It is of size 2 because it is declared that there are two derived classes, which can have overriding template functions, for the class “Base” in the line “VTF::enable_virtual_template_functions<D1,D2>”. The First function pointer will be to the function D1::f<int>(int), and the second to D::f<int>(int).
If we were to also instantiate Base::f<double>(int), then the table would be D1::f<double>(int) and D2::f<double>(int).
Reflection is used to search the derived classes for the correct overriding function. When we instantiate a virtual template function in the base class with some template parameters, we search each of the derived classes for a template function that has the same identifier, template parameter list, function parameter list, and return type. If we find such a template, we will instantiate it with the same template parameters as the base function was and store a function pointer to it. If there isn't, then we store a pointer to the default function.
This approach is essentially the inverse of how normal virtual functions work. With normal virtual function, each of the base and derived classes has a single vtable that contains all the pointers to the correct overriding functions. But with virtual template functions, each instantiated function has its own vtable of pointers to the correct functions from the derived classes.
In the provided example, the function parameter could be “T” instead of “int”. I didn't intend the template parameter to be used in the function parameter, but it doesn’t change anything. It would have been more understandable if, instead of a type template parameter, I used a non-type or template template parameter.
example: