Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板参数中的模板函数名_C++_Templates_Overloading - Fatal编程技术网

C++ 模板参数中的模板函数名

C++ 模板参数中的模板函数名,c++,templates,overloading,C++,Templates,Overloading,我一直在寻找与我的问题有关的例子,但我仍然找不到解决方案。我找到的最接近的东西是 如果需要,我将尝试发布一个工作示例,但到目前为止,我的部分代码涉及 以下: template<class InterfaceType, class T> inline void write_info(InterfaceType& interface, T& t) { InterfaceType::write_info(interface, t); } template<

我一直在寻找与我的问题有关的例子,但我仍然找不到解决方案。我找到的最接近的东西是

如果需要,我将尝试发布一个工作示例,但到目前为止,我的部分代码涉及 以下:

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);
}

正如你所看到的,我一遍又一遍地写着同样的代码。这里我假设InterfaceType已经为
int
double
定义了
write\u info
write\u data
write\u definition
。有什么想法吗?

改变逻辑:与其为每种类型编写专门的
write\u thing
重载,不如编写一个
apply
函数,将任意函数应用于每种类型的对象,然后为每个
write\u thing
编写一个重载,简单地委托给
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);
}
//定义一个处理“其他所有内容”的全面应用程序
模板
无效应用(接口和i、函数f、对象和x){
f(i,x);
}
//为处理特殊情况的“apply”定义重载
模板
无效应用(接口和i、函数f、函数O和x){
f(i,x.a);
f(i,x.b);
}
//为写[Uthing]函数定义多态适配器:
结构写入信息导入{
模板
void运算符()(接口和i、对象和x)常量{
接口::写入信息(i,x);
}
};
//然后根据上述内容实现write_uThing函数:
模板
无效写入信息(接口和接口、对象和x){
应用(i,write_info_impl(),x);
}

我喜欢这种逻辑,但编译器似乎不知道要使用哪个
&Interface::write_info
。显然,我们希望使用一个能够处理我们在
apply
中使用的类型的函数。有没有办法明确地告诉它我们正在使用
&Interface::write_info
?否则它会在函数的位置告诉
。@jmlopez:啊,是的。可以使用泛型函子封装重载集。请参阅更新。代码要多一点,但是如果
write\uthing]
函数都和它们看起来一样,你可以用一个宏生成它们。回答得好。1小时不投票?休息室一定有收据对不起,我花了一些时间才查到这个新密码。现在,它编译并运行到某一点。它适用于基本类型:
int
double
。但对于特殊情况
foo
,不使用正确的应用程序。当
对象
foo
时,编译器为什么会忽略重载
应用
?对,调用必须是非限定的(即没有任何
::
),以便根据模板参数类型使用参数相关的查找。每个
apply
函数都需要位于(a)定义
接口
的命名空间或(b)定义
对象
的命名空间(或其中一个的封闭命名空间,尽管通常最好将重载放在同一命名空间中)。
// 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);
}