模板参数推导(在同一调用中同时使用显式和隐式参数)

发布于 2024-10-13 17:01:36 字数 1122 浏览 4 评论 0原文

我有一个函数的三个模板参数,并且(我认为)编译器在推断哪个模板参数是哪个时遇到了麻烦。

模板函数是:

#include <structures/partition.h>
#include <vector>

namespace cliques
{
    template <typename P, typename T, typename QF>
    P find_optimal_partition_louvain(cliques::Graph<T> &my_graph,
                                    QF quality_function)
    {

        P dummy;

    }

}

当我尝试使用

cliques::find_optimal_partition_louvain<cliques::DisjointSetForest>(my_new_graph, cliques::find_linearised_stability(current_markov_time));

其中模板参数 P 应该对应于 cliques::DisjointSetForest 来调用它时,普通函数参数是模板化类和函数对象。

但是

error: no matching function for call to
find_optimal_partition_louvain(cliques::Graph<lemon::ListGraph>&,
cliques::find_linearised_stability)

,如果我使用内置类型(例如 int 或 float)作为 P 参数,则一切都会编译正常。

例如,

cliques::find_optimal_partition_louvain<int>(my_new_graph, cliques::find_linearised_stability(current_markov_time));

所以我的问题是我在这里做错了什么,我怎样才能更好地通知编译器哪个参数是哪个,或者我完全偏离了轨道?

I have three template arguments to a function and am having troubles with (I think) the compiler deducing which template argument is which.

The template function is:

#include <structures/partition.h>
#include <vector>

namespace cliques
{
    template <typename P, typename T, typename QF>
    P find_optimal_partition_louvain(cliques::Graph<T> &my_graph,
                                    QF quality_function)
    {

        P dummy;

    }

}

And when I try to call it with

cliques::find_optimal_partition_louvain<cliques::DisjointSetForest>(my_new_graph, cliques::find_linearised_stability(current_markov_time));

Where template argument P should correspond to cliques::DisjointSetForest, and the normal function arguments are a templated class and a function object.

This fails with

error: no matching function for call to
find_optimal_partition_louvain(cliques::Graph<lemon::ListGraph>&,
cliques::find_linearised_stability)

However if I use a builtin type such as an int or a float for the P parameter everything compiles fine.

e.g.

cliques::find_optimal_partition_louvain<int>(my_new_graph, cliques::find_linearised_stability(current_markov_time));

So my question is what am I doing wrong here, how can I use a better inform the compiler which parameter is which, or am I completely off track?

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

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

发布评论

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

评论(3

无声无音无过去 2024-10-20 17:01:36

我讨厌回答我自己的问题,但问题是 cliques::DisjointSubsetForest 实际上是一个模板类,所以

cliques::find_optimal_partition_louvain<cliques::DisjointSetForest<int> >(my_new_graph, cliques::find_linearised_stability(current_markov_time));

可以工作

I hate to answer my own question but problem was that cliques::DisjointSubsetForest is actually a templated class so

cliques::find_optimal_partition_louvain<cliques::DisjointSetForest<int> >(my_new_graph, cliques::find_linearised_stability(current_markov_time));

works

你的呼吸 2024-10-20 17:01:36

“错误:没有匹配的函数用于调用 'find_optimal_partition_louvain(cliques::Graph&, cliques::find_linearized_stability)”

看来您的编译器认为 cliques::Graph 不是模板。

"error: no matching function for call to ‘find_optimal_partition_louvain(cliques::Graph&, cliques::find_linearised_stability)"

It would seem that your compiler thinks that cliques::Graph is not a template.

故笙诉离歌 2024-10-20 17:01:36

我尝试在一个简单的示例中重现该错误,但我未能做到这一点(在 gcc 上)。

看起来编译器确实发现 find_optimal_partition_louvain 是一个函数模板。我建议尝试以下操作:

cliques::template find_optimal_partition_louvain<cliques::DisjointSetForest>(my_new_graph, cliques::find_linearised_stability(current_markov_time));

否则,您可能想验证以下简单示例是否可以在您的编译器上正常编译(因为它应该!):

#include <iostream>

template <class G>
struct Bar { };

namespace Foo {

template <class A, class B, class C>
A some_function(Bar<B>&, C aFunc) {
  aFunc();
  return A();
};

};

struct HWPrinter {
  HWPrinter() { std::cout << "Hello World!" << std::endl; };
};

struct IntPrinter {
  int value;
  IntPrinter(int aValue) : value(aValue) { };
  void operator() () { std::cout << "The integer is: " << value << std::endl; };
};

int main() {
  Bar<HWPrinter> ab;

  Foo::some_function<HWPrinter>(ab,IntPrinter(42));

  return 0;
};

I have tried to reproduce the error on a simple example, but I failed to do so (on gcc).

It looks as though the compiler does figure out that find_optimal_partition_louvain is a function template. I suggest trying the following:

cliques::template find_optimal_partition_louvain<cliques::DisjointSetForest>(my_new_graph, cliques::find_linearised_stability(current_markov_time));

Otherwise, you might want to verify if the following simple example compiles fine on your compiler (because it should!):

#include <iostream>

template <class G>
struct Bar { };

namespace Foo {

template <class A, class B, class C>
A some_function(Bar<B>&, C aFunc) {
  aFunc();
  return A();
};

};

struct HWPrinter {
  HWPrinter() { std::cout << "Hello World!" << std::endl; };
};

struct IntPrinter {
  int value;
  IntPrinter(int aValue) : value(aValue) { };
  void operator() () { std::cout << "The integer is: " << value << std::endl; };
};

int main() {
  Bar<HWPrinter> ab;

  Foo::some_function<HWPrinter>(ab,IntPrinter(42));

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