- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
从这里开始,(endo)仿函数是能够获取一个对象并将其转换为同一类型的另一个对象的东西。仿函数最简单的例子是恒等式:
struct Identity {
template <typename T>
T Apply(T x) {
return x
}
};
我需要一个标识通用仿函数的“仿函数类型”。我想做的是编写如下代码:
class Sum {
public:
Sum(Functor* f, Functor* g) :
f_(f),
g_(g) {}
template <typename T>
T Apply(T x) { return f_->Apply(x) + g_->Apply(x); }
private
Functor* f_;
Functor* g_;
};
我想到的第一个想法当然是使用虚拟类:
struct Functor {
template <typename T>
virtual T Apply(T x) = 0;
};
这种方法无法解决的问题是模板不能是虚拟的。
然后我尝试使用 C++ 概念。但是,如 Specifying a concept for a type that has a member function template using Concepts Lite 中所述和 C++ Concepts: Can I define a concept that is itself a template?不可能有一个“模板化概念”。
最后我偶然发现了How to achieve "virtual template function" in C++因此我想出了以下可能的实现方式:
struct Functor {
template <typename T>
T Apply(const T& x); // No more virtual!!!
};
// ... Identity and Sum declarations properly inheriting from Functor ...
template <typename T>
T Functor::Apply(T x) {
if (Identity* specialized =
dynamic_cast<Identity*>(this)) {
return specialized->Apply(x);
} else if (const Sum* specialized =
dynamic_cast<const Sum*>(this)) {
return specialized->Apply(x);
} else ...
}
尽管这是编译,但这不是最好的解决方案。主要问题是:性能和代码重复。性能问题来自这样一个事实,即每次在 Functor 上调用 Apply 时,必须解析 Functor::Apply 中的长 if 子句。这是一个大问题,因为 Functor 可以深度嵌套(因此调用 Apply 可能会导致多次调用 Functor::Apply)。 “代码重复”问题是不言而喻的,因为每次我想定义一个新的 Functor 时,我还必须修改 Functor::Apply 添加一个新的 if 子句。
我在这里想问的是,是否有合适(更清晰)的方法来定义 Functor 接口(interface)/概念,从而可以创建像 Sum 这样的类。接受 C++ 概念和繁重的模板元编程。
附注所有代码片段都特意保持尽可能简单。避免建议使用 class 而不是 struct 或添加 const 标识符或使用唯一指针,这不是这个问题的重点。
最佳答案
不幸的是,我能想到的大多数(最佳)解决方案都要求您采用一些相当复杂的方法。当然,这不一定是坏事,但是当您继续设计程序时,它会使事情变得困惑。出于这个原因,我可能会建议一些更直接的东西:
template <typename F, typename G>
class Sum {
public:
Sum(F& f, G& g) :
f_(f),
g_(g) {}
template <typename T>
inline T Apply(T x) { return f_.Apply(x) + g_.Apply(x); }
private:
F& f_;
G& g_;
};
/*
For every class like the above, you may want to define an
easy-to-use generating function to simplify instantiations:
*/
template <typename F, typename G>
inline Sum<F, G> MakeSum(F& f, G& g)
{
return Sum<F, G>(f, g);
}
#include <cmath>
struct SquareRoot {
template <typename T>
inline T Apply(T x)
{
return std::sqrt(x);
}
};
struct Triple {
template <typename T>
inline T Apply(T x)
{
return T(3) * x;
}
};
// Example:
#include <iostream>
int main(void)
{
using namespace std;
SquareRoot square_root;
Triple triple;
// For g++, don't forget to compile with -std=c++1z
auto sum = MakeSum(square_root, triple);
cout << sum.Apply(1024) << endl;
}
诚然,它不如其他技术强大,但它可能是一个很好的起点。
关于c++ - 内仿函数概念或接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40835599/
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 10年前关闭。 Improve this qu
我正在开发一个 Android 应用程序。在此应用程序中, Logo 栏显示在所有页面( Activity )上,或者我们可以说它在所有页面上都有标题。这个 Logo 栏有几个图标,如主页、登录、通知
我正在使用 hadoop 使用开源接口(interface) HVPI 处理视频。然而,inputsplit 的实现,更准确地说是在 isSplitableobContext (context, Pa
1. 是什么? MySQL 是最流行的关系型数据库管理系统,在 WEB 应用方面 MySQL 是最好的 RDBMS(Relational Database Management System
有没有办法使用 c++20s 的概念来检查一个值是否满足某些要求? 假设我正在编写某种使用分页的容器,并且我想让页面大小成为模板参数。 template class container; 我可以使用带
如何在 ArrayList 中循环遍历 ArrayList? 例如,如果我有一个名为 Plants of Plant 对象的 ArrayList。每个 Plant 对象内部都有一个随机数量的花名。我如
如何在UML类图中绘制C++概念? 具体来说,我有以下代码: template concept Printable = requires(T a, std::ostream &where) {
我有兴趣制作一个网站,在访问者访问时闪现整个网络历史记录。我计划使用 JavaScript 来获取每个观看者计算机上的历史记录,并根据他们拥有的内容以不同的速度对其进行动画处理。我的想法是使用 his
有一个模板定义,例如: template void foo( void ) { /* ... */ } 如何定义一个概念,以便N必须为非零正值(N> = 1)? 就像是: template con
封装是信息隐藏还是导致信息隐藏? 正如我们所说,封装将数据和函数绑定(bind)在单个实体中,因此它为我们提供了对数据流的控制,并且我们只能通过一些定义良好的函数来访问实体的数据。因此,当我们说封装导
下面有一个简单的代码片段,它使用以下方式进行编译: g++-9 -std=c++2a -fconcepts 这是试图定义一个需要存在函数的概念。我希望输出是"is",但事实并非如此……知道为什么吗?谢
我有一个普通二元运算符的概念 template concept is_binary_operation = requires (const T& t1, const T& t2) // e.g
我正在c++ 20中实现具有启发式功能的搜索算法。 我试图用类似这样的概念来约束我的算法可以使用的功能: template concept Heuristic = requires(SelfType
我需要了解 SAS 如何读取/执行数据步骤。当我查找有关 SAS 如何读取数据步骤的信息时,我似乎只找到有关它如何读取以进行合并的信息,我不了解与常规数据步骤相关的信息。比方说,我有这行代码: dat
最近我看到一个关于“框架”的问题,如果“框架”有不同的类型或概念。那么,存在不同“类型”的“框架”吗? 例如:NodeJS 是一种“类型”(概念),而 Hibernate ORM 是另一种“类型”(概
如何使用任何技术禁用或清除客户端浏览器 Cookie 我认为使用 javascript 可以用于任何技术 最佳答案 var cookies = document.cookie.split(";");
我正在使用 target = "_blank" 单击链接时生成新选项卡。但是,浏览器会将焦点移至该选项卡。 有没有办法让焦点保持在当前标签页上? 回答摘要 基本上,只需发送一个模拟控件点击的当前事件。
我正在尝试在我的 android/firebase(cloud firestore) 应用程序上添加一项需要其他用户批准/拒绝的功能。例如,当 Air&BnB 上的用户想要预订一个地方时,所有者必须批
这个问题在这里已经有了答案: mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expec
public class MyClass { public static void main(String[] args) { System.out.println("Hell
我是一名优秀的程序员,十分优秀!