模板函数名称 [英] template function name in terms of template argument
问题描述
我一直在寻找与我的问题相关的例子,我仍然找不到解决方案。我发现的最接近的是
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
我将尝试发布一个工作示例以防万一它需要,但到目前为止我的代码的一部分涉及
如下:
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);
}
注意模板 write_info
依赖于具有 write_info
(静态方法)的接口类型。这样做的原因是因为 write_info
函数可以将
稍后专门用于特定数据类型,而不必重新定义 InterfaceType
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
.
简单的问题是:我们可以用一个模板来命名函数作为函数参数来减少上面的代码吗?请记住,我真的希望这是可能的,所以我可以避免为一个专门的数据类型定义所有这3个函数,即
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.
假设 foo
是一个具有两个属性 int a
和 double b
的结构。然后我可以专门化上面的函数像这样:
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);
}
正如你所看到的,我一遍又一遍地写相同的代码。这里我假设InterfaceType已经定义了 write_info
, write_data
和 write_definition
int
和 double
。任何想法?
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?
推荐答案
转动逻辑:而不是写专门的 write_thing
重载每个类型,写一个 apply
函数,将一个任意函数应用到每个类型的对象,然后每个 write_thing
只需委托 apply
:
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);
}
这篇关于模板函数名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!