简体   繁体   English

指向成员变量和模板

[英]Points to member variables and templating

I am currently using a templated function to evaluate the derivatives of mathematical functions, like so 我目前正在使用模板化函数来评估数学函数的导数,就像这样

template <class func_type>
arma::mat matrixDerivative
   (func_type func, const double x, double h, double b=1.4) {
      // <implementation details>
}

The template parameter allows me to use either function pointers or functors to evaluate the right-hand side. template参数允许我使用函数指针或函子来评估右侧。

Is there an easy way to extend this to evaluate the derivatives of functions that are class methods? 有没有简单的方法可以扩展它,以评估作为类方法的函数的派生类? I wasn't able to wrap my head around the use of function pointers to member functions and I couldn't work out the details of having a functor as a class attribute that still had access to its parent's attributes and methods. 我无法全神贯注地使用指向成员函数的函数指针,也无法解决将函子作为类属性仍然可以访问其父级的属性和方法的细节。

My questions usually aren't clear, so feel free to ask for clarifications in the comments before answering. 我的问题通常不清楚,因此在回答之前,请随时在评论中要求澄清。

In C++11 simply use a lambda: 在C ++ 11中,只需使用lambda:

[&]( double x )->double {
  return ptr->method(x);
}

which generates a function object that can be invoked with double . 生成一个可以用double调用的函数对象。 The above construct assumes that the lambda will be used and discarded before the end of the current scope ( [&] is unsafe otherwise). 上面的构造假定将在当前作用域结束之前使用和丢弃lambda(否则[&]是不安全的)。

Note that ->double can be omitted for single-line and void returning lambdas in C++11, and omitted even on multi-line lambdas in C++1y if you are ok with the return type it deduces (based off the first return statement in your function). 请注意,对于C ++ 11中的单行和void返回lambda,可以省略->double ,而对于C ++ 1y中的多行lambda,也可以省略->double如果您可以确定其推导的返回类型(基于第一个)函数中的return语句)。

In C++03, std::mem_fun can be used, or you can create a custom function object. 在C ++ 03中,可以使用std::mem_fun ,也可以创建一个自定义函数对象。

In C++1y: 在C ++ 1y中:

[&](auto&&... x) {
  return ptr->method(std::forward<decltype(x)>(x)...);/
}

creates a perfect-forwarding functor wrapper. 创建一个完美转发的函子包装。 I would rarely do that kind of thing, even in C++1y, outside of seriously industrial library code. 在认真的工业库代码之外,即使在C ++ 1y中,我也很少这样做。 Less verbose we get: 不再那么冗长:

[&](auto&&... x) {
  return ptr->method(x...);
}

which lets us imperfectly forward, and defer selection of the overload to the point of use of the lambda, not the point where the lambda was written, which can be nice. 这可以让我们不完美地前进,并将重载的选择推迟到lambda的使用点,而不是lambda的编写点,这可能很好。 (if any of the methods use rvalue or by-value calls, going back to perfect forwarding becomes tempting) (如果任何方法使用右值或按值调用,那么回到完美的转发就很诱人)

In C++11, std::mem_fn requires that the method in question not be overloaded, or that the overload be manually resolved (with an arcane-looking cast). 在C ++ 11中, std::mem_fn要求所讨论的方法不要重载,或者必须手动解决重载(使用看起来像奥秘的强制转换)。 std::bind then wraps that with a perfect-forwarding function object. 然后, std::bind用一个完美转发的函数对象包装它。 The main advantage this procedure is is that the type of the construct, while implementation defined, can be determined, so you can have a function that returns the type in question. 该过程的主要优点是,在定义实现的同时,可以确定构造的类型,因此您可以使用一个函数来返回所讨论的类型。 However, that is a pretty esoteric advantage: I would go with the lambda. 但是,这是一个非常深奥的优势:我会选择lambda。

Lambdas, while strange, are increasingly common in C++, and are easier to understand what they are doing than a chain bind mem_fun expression in my experience as they look more like "normal" code (after you get over the "magic"). Lambdas虽然很奇怪,但在C ++中却越来越常见,在我的经验中,它们比链bind mem_fun表达式更容易理解它们在做什么,因为它们看起来更像是“普通”代码(在克服“魔术”之后)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM