I've been looking for examples that relate to what my question is about and I still cannot find a solution. The closest thing I've found is
Template function as a template argument
I will try to post a working example in case it is needed but so far part of my code involves the following:
template<class InterfaceType, class T>
inline void write_info(InterfaceType& interface, T& t) {
InterfaceType::write_info(interface, t);
}
template<class InterfaceType, class T>
inline void write_data(InterfaceType& interface, T& t) {
InterfaceType::write_data(interface, t);
}
template<class InterfaceType, class T>
inline void write_definition(InterfaceType& interface, T& t) {
InterfaceType::write_definition(interface, t);
}
Notice that the templates write_info
depend on an interface type which has a method called write_info
(A static method). The reason this is done is because the write_info
function can
be specialized later on for an specific datatype without having to redefine anything on the InterfaceType
.
The simple question is: Can we reduce the above code with a template that names the function as a function parameter? Keep in mind that I really want this to be possible so that I can avoid defining all those 3 function for a specialized datatype, i.e.
Suppose that foo
is a structure with two attributes int a
and double b
. Then I can specialize the above functions like this:
template<class InterfaceType>
inline void write_info(InterfaceType& interface, foo& t) {
InterfaceType::write_info(interface, t.a);
InterfaceType::write_info(interface, t.b);
}
template<class InterfaceType>
inline void write_data(InterfaceType& interface, foo& t) {
InterfaceType::write_data(interface, t.a);
InterfaceType::write_data(interface, t.b);
}
template<class InterfaceType>
inline void write_definition(InterfaceType& interface, foo& t) {
InterfaceType::write_definition(interface, t.a);
InterfaceType::write_definition(interface, t.b);
}
As you can see I'm writing the same code over and over again. Here I'm assuming that the InterfaceType already has define write_info
, write_data
and write_definition
for int
and double
. Any ideas?