为什么在用于重载解析的函数参数中的const限定符? [英] Why are const qualifiers in function arguments used for overloading resolution?

查看:143
本文介绍了为什么在用于重载解析的函数参数中的const限定符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

使用const参数和重载的函数

很困惑的重载和const声明规则。
这两个东西让我难以想象,也许你可以帮我找到我头脑中更深的误解,导致他们对我迷惑。 ;)

I am pretty confused by the overloading and const declaration rules. Here are two things that puzzle me maybe you can help me find the deeper misunderstanding in my head that result in them being puzzling to me. ;)

第一期:

我的编译器允许这样:

void f(int & x) {
  std::cout << "plain f" << std::endl;
}
void f(const int & x) {
  std::cout << "const f" << std::endl;
}


$ b

But the following causes a compile error (function already has a body):

void f(int x) {
  std::cout << "plain f" << std::endl;
}
void f(const int x) {
  std::cout << "const f" << std::endl;
}

我认为这是有意义的,因为我认为const只是在那里告诉编译器,正在传递的对象不会更改,在第二种情况下,它仍然被复制。但是如果这是正确的,为什么我可以重载函数使用const?

Which I suppose makes sense because I thought the const was only there to tell the compiler that the object being passed is not changed and in the second case it is copied anyway. But if that is correct then why can I overload functions using const?

换句话说,为什么如果我使用编译版本并调用这样的函数:

In other words, why if I use the compiling version and call the functions like this:

  int x1 = 5;
  const int x2 = 5;
  f(x1);
  f(x2);

我得到plain f和const f而不是const f显然现在我也使用const告诉编译器哪个函数调用不仅参考不改变。这变得更混乱,因为如果我删除普通版本它工作正常,并调用const版本两次。

do I get "plain f" and "const f" instead of "const f" twice? Apparently now I am also using the const to tell the compiler which function to call not only that the reference doesn't change. This gets more confusing because if I remove the "plain" version it works just fine and calls the "const" version twice.

现在我的实际问题是什么?

Now what is my actual question? I would like to know what the ideas behind this behavior are because otherwise memorizing it is very hard.

推荐答案


我认为const只是在那里告诉编译器,
传递的对象不会改变,在第二种情况下它仍然被复制

I thought the const was only there to tell the compiler that the object being passed is not changed and in the second case it is copied anyway

您是正确的。因为在第二种情况下,它被复制,所以 const 对调用者没有区别,标准定义 void f(const int x) void f(int x)具有相同的签名。因为它们碰撞,你试图定义相同的函数两次。

You are correct. Because in the second case it's copied anyway, and so the const makes no difference to the caller, the standard defines that void f(const int x) and void f(int x) have the same signature. Hence they collide, you're trying to define the same function twice.

因为在第一种情况下不是 code> void f(const int& x)和 void f(int& x) em>签名。因此,它们过载。

Because in the first case it isn't copied anyway, void f(const int &x) and void f(int &x) have different signatures. Hence they overload.

在第一种情况下,禁止调用 int& c> f 与 x2 作为参数,因为这将创建一个非const引用const对象没有任何明确的转换。这样做违反了const系统的目的(这是如果你想打破const安全,你必须明确地使用cast)。因此,使用引用参数的函数的const和非const重载是有意义的。

In your first case, it's forbidden to call the int& version of f with x2 as argument, because that would create a non-const reference to a const object without any explicit cast in sight. Doing that defeats the purpose of the const system (which is that if you want to break const-safety, you have to do so explicitly with a cast). So it makes sense to have const- and non-const overloads of functions with reference parameters.

在第二种情况下,源的常量的副本,以及目的地的常量。你可以从非const变量初始化一个const变量,或者从const变量初始化一个非const变量。这不会导致问题,也不会破坏const安全。这就是为什么标准通过定义你的两个不同的版本的 f 实际上是同一个函数,帮助澄清这一点。

In your second case, there's no relation between the const-ness of the source of a copy, and the const-ness of the destination. You can initialize a const variable from a non-const one, or a non-const one from a const one. This causes no problems and doesn't break const-safety. That's why the standard helpfully makes this clear by defining that your two "different" versions of f are actually the same function.

这篇关于为什么在用于重载解析的函数参数中的const限定符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆