小编Ven*_*iva的帖子

使用C++的高级类型

这个问题适用于了解Haskell(或任何其他支持高级类型的函数语言)和C++的人...

是否可以使用C++模板对更高级的kinded类型进行建模?如果是,那怎么样?

编辑:

演示文稿由托尼·莫里斯:

高阶多态性:

  • Java和C#等语言具有一阶多态性,因为它们允许我们对类型进行抽象.例如, List<A>可以有一个reverse适用于任何元素类型(A)的函数 .

  • 更实用的编程语言和类型系统允许我们在类型构造函数上进行抽象.

  • 此功能称为高阶(或更高阶)多态.

示例:

伪Java,发明了高阶多态性的符号

interface Transformer<X, Y> {
  Y transform(X x);
}

interface Monad<M> { // M :: * -> *
  <A> M<A> pure(A a);
  <A, B> M<B> bind(Transformer<A, M<B>> t, M<A> a);
}
Run Code Online (Sandbox Code Playgroud)

c++ templates haskell higher-kinded-types

45
推荐指数
1
解决办法
7032
查看次数

boost :: ref和常规引用之间的区别

boost::ref(i)和之间有什么区别& i?在哪些情况下我们不能使用常规参考而必须boost::ref改为使用?如果可能,请提供示例.

c++ boost reference

9
推荐指数
1
解决办法
5371
查看次数

boost :: lambda表达式无法编译

我尝试编写一个函数,使用boost lambda库计算两个代码字之间的汉明距离.我有以下代码:

#include <iostream>
#include <numeric>
#include <boost/function.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/if.hpp>
#include <boost/bind.hpp>
#include <boost/array.hpp>

template<typename Container>
int hammingDistance(Container & a, Container & b) {
  return std::inner_product(
    a.begin(),
    a.end(),
    b.begin(),
    (_1 + _2),
    boost::lambda::if_then_else_return(_1 != _2, 1, 0)
  );
}

int main() {
  boost::array<int, 3> a = {1, 0, 1}, b = {0, 1, 1};
  std::cout << hammingDistance(a, b) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

而我得到的错误是:

HammingDistance.cpp: In function ‘int hammingDistance(Container&, Container&)’:
HammingDistance.cpp:15: error: no match for ‘operator+’ in ‘<unnamed>::_1 + <unnamed>::_2’ …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-lambda

4
推荐指数
1
解决办法
2223
查看次数

std :: binary_function - 不匹配呼叫?

包括

#include <functional>

using namespace std;

int main() {
  binary_function<double, double, double> operations[] = {
    plus<double>(), minus<double>(), multiplies<double>(), divides<double>() 
  };
  double a, b;
  int choice;
  cout << "Enter two numbers" << endl;
  cin >> a >> b;
  cout << "Enter opcode: 0-Add 1-Subtract 2-Multiply 3-Divide" << endl;
  cin >> choice;
  cout << operations[choice](a, b) << endl;
}
Run Code Online (Sandbox Code Playgroud)

而我得到的错误是:

Calcy.cpp: In function ‘int main()’:
Calcy.cpp:17: error: no match for call to ‘(std::binary_function<double, double, double>) (double&, double&)’
Run Code Online (Sandbox Code Playgroud)

任何人都可以解释为什么我得到这个错误以及如何摆脱它?

c++ stl

4
推荐指数
1
解决办法
1388
查看次数