模板参数推导(在同一调用中同时使用显式和隐式参数)
我有一个函数的三个模板参数,并且(我认为)编译器在推断哪个模板参数是哪个时遇到了麻烦。
模板函数是:
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我讨厌回答我自己的问题,但问题是 cliques::DisjointSubsetForest 实际上是一个模板类,所以
可以工作
I hate to answer my own question but problem was that cliques::DisjointSubsetForest is actually a templated class so
works
“错误:没有匹配的函数用于调用 '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.
我尝试在一个简单的示例中重现该错误,但我未能做到这一点(在 gcc 上)。
看起来编译器确实发现
find_optimal_partition_louvain
是一个函数模板。我建议尝试以下操作:否则,您可能想验证以下简单示例是否可以在您的编译器上正常编译(因为它应该!):
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:Otherwise, you might want to verify if the following simple example compiles fine on your compiler (because it should!):