简体   繁体   English

需要帮助如何将C ++自动值传递给函数

[英]Need help how to pass a c++ auto value to a function

Here is my sample code shrunk down from the original and this compiles on my Mac: 这是我的示例代码从原始代码缩减而来,并且可以在Mac上编译:

#include <cmath>
#include <iostream>
#include <random>
#include <string>
#include <vector>
#include <Array>

class ranker {};

namespace detail {
  template <class T>
  struct unique_if
  {
    using single_object = std::unique_ptr<T>;
  };

  template <class T>
  struct unique_if<T[]>
  {
    using unknown_bound = std::unique_ptr<T[]>;
  };
}

/**
 * Constructs a unique ptr in place.
 * @param args The parameters to the constructor
 * @return a unique_ptr<T>
 */
template <class T, class... Args>
typename detail::unique_if<T>::single_object make_unique(Args&&... args)
{
  return std::unique_ptr<T>{new T(std::forward<Args>(args)...)};
}

template <class T>
typename detail::unique_if<T>::unknown_bound make_unique(std::size_t size)
{
  return std::unique_ptr<T>{new typename std::remove_extent<T>::type[size]()};
}

class MPtf2ln_ranker {
  private: // Change the parameters to suit your ranking function
    double lambda_ = 0;
    double alpha_ = 0;
    double s_ = 0;
    double mu_ = 0;

  public:
    MPtf2ln_ranker(); // Default Constructor
    MPtf2ln_ranker(double lambda, double alpha, double s, double mu); // Constructor
    void set_param(double lambda, double alpha, double s, double mu) {
        lambda_ = lambda; alpha_ = alpha; s_ = s; mu_ = mu;
    }; // Sets the parameters
    // Calculates the score for one matched term
    double score_one();
};//MPtf2ln_ranker CLASS

MPtf2ln_ranker::MPtf2ln_ranker(){}//MPtf2ln_ranker()
MPtf2ln_ranker::MPtf2ln_ranker(double lambda, double alpha, double s, double mu) 
: lambda_{lambda}, alpha_{alpha}, s_{s}, mu_{mu} {
}//MPtf2ln_ranker()
double MPtf2ln_ranker::score_one() {
  return 1.1;
}//score_one()

void tune(  ) {
  std::array<double,11> lambdaValues {{ 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0 }};
  std::array<double,11> alphaValues  {{ 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0 }};
  std::array<double,11> sValues      {{ 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0 }};
  std::array<double,10> muValues     {{ 0.0, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500 }};

  auto ranker = make_unique<MPtf2ln_ranker>();
  std::size_t s, m, l, a;
  s = 0; m=0; l=0; a=0;
  ranker->set_param( lambdaValues[l], alphaValues[a], sValues[s], muValues[m]);     
}

int main(int argc, char* argv[]) {
  auto ranker = make_unique<MPtf2ln_ranker>();

  tune();
  return 0;
}

In the main() the ranker needs to be passed to the tune() function. 在main()中,需要将排名传递给tune()函数。 In tune() function I want to comment out its auto ranker variable and use the one that is passed in. This would allow the tune() to handle multiple rankers and not have to duplicate my code. 在tune()函数中,我想注释掉它的自动等级变量,并使用传入的变量。这将允许tune()处理多个等级,而不必重复我的代码。

Hopefully someone would be willing to tell me how to do this. 希望有人愿意告诉我该怎么做。 Thanks 谢谢

You can use a template 您可以使用模板

template <typename T>
void tune(unique_ptr<T>& p) { /* ... */ }

[...]
tune(ranker);

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

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