编译器如何确定使用哪个函数?

时间:2021-10-28 16:45:16

As we know compiler determines the function by the overloaded function signature (like parameter types), but how does this one work:

我们知道编译器通过重载函数签名(如参数类型)来确定函数,但是这个函数如何工作:

v[i] = 1;

When compiler looks at these two overloaded functions:

当编译器查看这两个重载函数时:

const T& operator[](size_t i) const;
T& operator[](size_t i);

How does it determine which one to use? Does the compiler tries to use 1st one, and finds out it does not work, then it tries to use the second one?

它如何确定使用哪一个?编译器是否尝试使用第一个,并发现它不起作用,然后它尝试使用第二个?

2 个解决方案

#1


3  

If the object is non-const, the non-const version of the function is invoked (if it is available), else const version is invoked. Now see which is which:

如果对象是非const,则调用函数的非const版本(如果可用),否则调用const版本。现在看看哪个是:

const T& operator[](size_t i) const;   //CONST MEMBER FUNCTION
T& operator[](size_t i);               //NON-CONST MEMBER FUNCTION

An example,

void f(std::vector<int> const &v1, std::vector<int> & v2)
{
   std::cout << v1[0] << std::endl; //invokes const version    
   std::cout << v2[0] << std::endl; //invokes non-const version
}

Now when you write:

现在你写的时候:

v[i] = 1; 

If I don't assume v to be std::vector, then it depends on the const-ness of the v. If v is const, then const-version will be invoked, else non-const version will be invoked (if it is available, else v will convert into const object and then const function will be invoked).

如果我不假设v是std :: vector,那么它取决于v的const。如果v是const,那么将调用const-version,否则将调用非const版本(如果它是可用的,否则v将转换为const对象,然后将调用const函数)。

#2


1  

The non-const member function cannot be called on a const object.

无法在const对象上调用非const成员函数。

Hence, in order to be practically useful, the rules have to be that it's called on a non-const object, and that conversely, the const member function is called on a const object.

因此,为了实际有用,规则必须是在非const对象上调用它,相反,const成员函数在const对象上调用。

And that's what the rules are.

这就是规则。

#1


3  

If the object is non-const, the non-const version of the function is invoked (if it is available), else const version is invoked. Now see which is which:

如果对象是非const,则调用函数的非const版本(如果可用),否则调用const版本。现在看看哪个是:

const T& operator[](size_t i) const;   //CONST MEMBER FUNCTION
T& operator[](size_t i);               //NON-CONST MEMBER FUNCTION

An example,

void f(std::vector<int> const &v1, std::vector<int> & v2)
{
   std::cout << v1[0] << std::endl; //invokes const version    
   std::cout << v2[0] << std::endl; //invokes non-const version
}

Now when you write:

现在你写的时候:

v[i] = 1; 

If I don't assume v to be std::vector, then it depends on the const-ness of the v. If v is const, then const-version will be invoked, else non-const version will be invoked (if it is available, else v will convert into const object and then const function will be invoked).

如果我不假设v是std :: vector,那么它取决于v的const。如果v是const,那么将调用const-version,否则将调用非const版本(如果它是可用的,否则v将转换为const对象,然后将调用const函数)。

#2


1  

The non-const member function cannot be called on a const object.

无法在const对象上调用非const成员函数。

Hence, in order to be practically useful, the rules have to be that it's called on a non-const object, and that conversely, the const member function is called on a const object.

因此,为了实际有用,规则必须是在非const对象上调用它,相反,const成员函数在const对象上调用。

And that's what the rules are.

这就是规则。