template function name in terms of template argume

2019-03-02 11:09发布

问题:

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?

回答1:

Turn the logic around: rather than writing specialized write_thing overloads for each type, write a single apply function that applies an arbitrary function to an object of each type, then have a single overload of each write_thing that simply delegates to the apply:

// Define a catch-all apply that handles "everything else"
template <typename Interface, typename Function, typename Object>
void apply(Interface& i, Function f, Object& x) {
    f(i, x);
}

// Define overloads for "apply" that handle special cases
template <typename Interface, typename Function>
void apply(Interface& i, Function f, foo& x) {
    f(i, x.a);
    f(i, x.b);
}

// Define polymorphic adapters for your write_[thing] functions:
struct write_info_impl {
    template <typename Interface, typename Object>
    void operator()(Interface& i, Object& x) const {
        Interface::write_info(i, x);
    }
};

// Then implement your write_[thing] functions in terms of the above:
template <typename Interface, typename Object>
void write_info(Interface& interface, Object& x) {
    apply(i, write_info_impl(), x);
}