如何在不使用 C++0x auto 的情况下实现这种类型特定的对象生成器

发布于 2024-10-30 18:57:38 字数 736 浏览 2 评论 0原文

我有一个模板化函数,其中模板化参数是一个函子。我想生成一些特定于特定质量函数的内部变量,供从此函子调用的其他模板函数使用。所以我尝试了 auto 关键字,它似乎可以工作,

template<typename QF>
float find_optimal_partition_louvain_with_gain(QF quality_function) {
    auto internal_variables = gen(quality_function);
    float result = do_some_work(internal_variables);
    ...
    return result;
}

struct internals_for_linearised_functor {
    double x;
};

internals_for_linearised_functor gen(mynamespace::linearised_function &f) {
    linearised_internals internals;
    internals.x = 5;
    return internals;
}

使用 auto 似乎编译得很好,这令人高兴。我想尝试保持在当前标准范围内,以使我的代码更加可移植。有没有办法使用当前标准来处理这个问题,或者这是 BOOST_AUTO 能够处理的事情?我试图实现的一般功能是根据模板化参数的类型生成一个对象,而不实际将新对象的类型传递到函数中(即推断它)

谢谢

I have a templated function where the templated parameter is a functor. I want to generate some internal variables, specific to a particular quality function, to be used by other template functions called from this functor. so I have experimented with the auto keyword and it seems to work

template<typename QF>
float find_optimal_partition_louvain_with_gain(QF quality_function) {
    auto internal_variables = gen(quality_function);
    float result = do_some_work(internal_variables);
    ...
    return result;
}

struct internals_for_linearised_functor {
    double x;
};

internals_for_linearised_functor gen(mynamespace::linearised_function &f) {
    linearised_internals internals;
    internals.x = 5;
    return internals;
}

With auto this seems to compile fine, which is pleasing. I would like to try to stay within the current standard though to make my code more portable. Is there a way this can be handled using the current standard, or is this something that BOOST_AUTO will be able to handle? The general function I am trying to achieve is generate an object based on a on the type of templated paramater without actually passing the type of the new object into the function (i.e. inferring it)

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

溇涏 2024-11-06 18:57:38

这应该是开箱即用的:

template<typename QF>
float find_optimal_partition_louvain_with_gain(QF quality_function) {
    return do_some_work(gen(quality_function));
}

如果您需要 internal_variables 的存在时间比仅一次调用的时间长,您可以使用另一个小函数来推断类型:

template<typename QF, typename T>
float find_optimal_partition_louvain_with_gain_detail(QF quality_function, T internal) {
    float result = do_some_work(internal);
    /* use internal and quality_function again here!.*/
    return result;
}


template<typename QF>
float find_optimal_partition_louvain_with_gain(QF quality_function) {
    return find_optimal_partition_louvain_with_gain_detail(
      quality_function,
      gen(quality_function)
    );
}

希望这会有所帮助!

This should work out of the box:

template<typename QF>
float find_optimal_partition_louvain_with_gain(QF quality_function) {
    return do_some_work(gen(quality_function));
}

If you need internal_variables to be around longer than for just one call, you can use another small function to deduce the type:

template<typename QF, typename T>
float find_optimal_partition_louvain_with_gain_detail(QF quality_function, T internal) {
    float result = do_some_work(internal);
    /* use internal and quality_function again here!.*/
    return result;
}


template<typename QF>
float find_optimal_partition_louvain_with_gain(QF quality_function) {
    return find_optimal_partition_louvain_with_gain_detail(
      quality_function,
      gen(quality_function)
    );
}

Hope this helps!

⊕婉儿 2024-11-06 18:57:38

如果您不想在这种情况下使用 auto,则需要使用某种元函数(可能是函数特征)来获取 gen< 的返回类型/代码>。

Boost 具有函数特征,或者您可以专门为您的 QF 类型实现一个特征类。如果它是标准函数对象类型,它应该有一个 result_type typedef,您可以使用 typedef typename QF::result_type result_type 获得它

If you don't want to use auto in this case, you'll need to use some kind of meta-function (a function trait, perhaps) to get the return type of gen.

Boost has function traits, or you could implement a traits class specifically for your QF type. If it's a standard function object type, it should have a result_type typedef, which you can get at using typedef typename QF::result_type result_type

私野 2024-11-06 18:57:38

您可以将 gen() 作为静态方法包装在结构中,然后替换 internal_variables 吗?请参阅以下修改后的代码。

struct Generate
{
  static internals_for_linearised_functor s_Internals; // define somewhere
  static void gen(mynamespace::linearised_function &f)
  {
    linearised_internals internals;
    internals.x = 5;
    s_Internals = internals;
  }
 };

现在,在函数 find_optimal_partition_louvain_with_gain 中,您可以首先调用 Generate::gen(),然后使用 Generate::s_Internals 代替 内部变量

这似乎是避免了解 auto 类型的更简单方法。
此外,如果需要,您可以将 Generate 设置为 template
对于上面代码中的线程安全,我们也可以在没有static的情况下实现。

Can you wrap gen() as static method inside a struct and then replace the internal_variables ? See the following modified code.

struct Generate
{
  static internals_for_linearised_functor s_Internals; // define somewhere
  static void gen(mynamespace::linearised_function &f)
  {
    linearised_internals internals;
    internals.x = 5;
    s_Internals = internals;
  }
 };

Now, in function find_optimal_partition_louvain_with_gain, you can first call Generate::gen() and then use, Generate::s_Internals instead of internal_variables.

This seems to be easier way to avoid knowing the type of auto.
Also, you can make Generate as template<> if needed.
For thread safety in above code, we can implement without static also.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文