在C++中解决模棱两可的调用

huangapple go评论71阅读模式
英文:

Resolving an ambiguous call in C++

问题

我有一个问题,我想通过交换默认参数来重载两个函数,以便在调用中可以交换它们。

Create(UncachedGraph* graph, std::string name, Node* parent, std::string path = "", int pos = -1);
Create(UncachedGraph* graph, std::string name, Node* parent, int pos = -1, std::string path = "");

但问题是,如果在调用中不传递任何参数给默认参数,调用就会产生歧义。

Create(this->graph, this->node->getName(), this->node->getParent()); // 由于无法解析要调用的重载,Create(graph, name, parent)存在歧义

我理解为什么会这样。但我真的很希望通过某种规则,比如使用某种“魔法限定符”,以优先考虑一种重载而不是另一种来解决这个问题。

prioritized Create(UncachedGraph* graph, std::string name, Node* parent, std::string path = "", int pos = -1);
Create(UncachedGraph* graph, std::string name, Node* parent, int pos = -1, std::string path = "");

在上面的调用中,通过调用第一个重载来解决歧义,或者其他一些方法。问题是,在这种情况下,我根本不关心调用哪个重载,因为它们是完全相同的。

所以,有没有可能解决这个问题的方法?

英文:

I have a problem where I want to overload 2 functions by swapping the default parameters to make it possible to swap them in a call.

Create(UncachedGraph* graph, std::string name, Node* parent, std::string path = "", int pos = -1);
Create(UncachedGraph* graph, std::string name, Node* parent, int pos = -1, std::string path = "");

but the problem is that if you call it without passing any arguments to default parameters, the call is ambiguous

Create(this->graph, this->node->getName(), this->node->getParent()); // Create(graph, name, parent) is ambiguous since the overload to be called cannot be resolved

and I understand why it is like that. But I would really love to somehow resolve it by prioritizing one overload over another by some rule, like for example with some "magic qualifier"

prioritized Create(UncachedGraph* graph, std::string name, Node* parent, std::string path = "", int pos = -1);
Create(UncachedGraph* graph, std::string name, Node* parent, int pos = -1, std::string path = "");

which in the above call would resolve the ambiguity by calling the first overload, or something else. The thing is, in this case I simply do not care which of the overloads is being called, because there they are just the same.

So, is there any possible thing I can do to resolve this problem?

答案1

得分: 0

以下是翻译好的部分:

but the problem is that if you call it without passing any arguments to default parameters, the call is ambiguous

问题在于,如果在调用时没有传递任何参数给默认参数,调用会产生歧义。

Well, to resolve ambiguity at least one of the parameters in one of the function declarations must be defined without default:

嗯,为了解决歧义,至少在其中一个函数声明中,必须定义一个参数而不使用默认值:

Create(UncachedGraph* graph, std::string name, Node* parent,  
       std::string path = "", int pos);
Create(UncachedGraph* graph, std::string name, Node* parent,  
       int pos = -1, std::string path = "");

The thing is, in this case I simply do not care which of the overloads is being called, because there they are just the same.

问题是,在这种情况下,我根本不关心哪个重载函数被调用,因为它们是完全相同的。

You can simply implement the other function with all defaults to call the other one, using parameters in right order:

您可以简单地实现另一个函数,将所有默认参数都传递给另一个函数,按正确的顺序使用参数:

Create(UncachedGraph* graph, std::string name, Node* parent,  
       int pos = -1, std::string path = "") {
    Create(graph, name, parent, path, pos);
}
英文:

> but the problem is that if you call it without passing any arguments to default parameters, the call is ambiguous

Well, to resolve ambiguity at least one of the parameters in one of the function declarations must be defined without default:

Create(UncachedGraph* graph, std::string name, Node* parent,  
       std::string path = "", int pos);
Create(UncachedGraph* graph, std::string name, Node* parent,  
       int pos = -1, std::string path = "");

> The thing is, in this case I simply do not care which of the overloads is being called, because there they are just the same.

You can simply implement the other function with all defaults to call the other one, using parameters in right order:

Create(UncachedGraph* graph, std::string name, Node* parent,  
       int pos = -1, std::string path = "") {
    Create(graph, name, parent, path, pos);
}

huangapple
  • 本文由 发表于 2023年5月21日 20:06:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76299833.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定