我一直致力于一个名为C++ 11/14的功能编程特性的项目(我的大学之一).关于这些主题有几个现有的来源和类似的演示文稿,我发现不久前的一个包含几个我完全无法理解的代码片段(并且不知何故它们可以连接到函数式编程).片段A和B属于递归,C属于懒惰评估.我想在下面与您分享:
片段A:
#include <iostream>
template <int N>
struct Factorial {
static int const val = N * Factorial<N - 1>::val;
};
template <>
struct Factorial <0> {
static int const val = 1;
};
int main() {
int factorial_of_6 = Factorial<6>::val;
std::cout << factorial_of_6 << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这里的重点是编译时评估(为了避免运行时计算并提高性能)?或者还有其他优点吗?
代码片段B:
#include <iostream>
template <int ...>
struct my_sum;
template <>
struct my_sum <> {
static …Run Code Online (Sandbox Code Playgroud) c++ recursion functional-programming variadic-templates c++11
经过几个小时的研究(在MSDN网站等),我没有找到为什么通用Dictionary<TKey, TValue>不提供ForEach()像List<T>这样的方法.有人可以给我一个解释吗?(我知道将它作为扩展方法实现并不难,这里可以看到一个很好的例子,我只是想知道为什么它首先不是由.NET库提供的特定原因.)
提前致谢.