相关文章推荐
绅士的机器人  ·  Git ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

The STL is full of definitions like this:

iterator begin ();
const_iterator begin () const;

As return value does not participate in overloading resolution, the only difference here is the function being const. Is this part of the overloading mechanism? What is the compiler's algorithm for resolving a line like:

vector<int>::const_iterator it = myvector.begin();

The compiler's "algorithm" is like this: Every member function of class X has an implicit argument of type X& (I know, most think it's X*, but the standard states, that for purposes of Overload Resolution we assume it to be a reference). For const functions, the type of the argument is const X&. Thus the algorithm, if a member function is called the two versions, const and non-const, are both viable candidates, and the best match is selected just as in other cases of overload resolution. No magic :)

thanks, this is useful, however my problem was the (wrong) assumption that the const variant is called because of the type of the variable it is assigned to. @awoodland explained this – davka Feb 24, 2011 at 11:18 Why reference and not pointer and that too only for overload resolution, any non-obvious reason for it ? – Talespin_Kit Jan 30, 2018 at 5:07

Yes, the const modifier affects overloading. If myvector is const at that point const version will be called:

void stuff( const vector<int>& myvector )
    vector<int>::const_iterator it = myvector.begin(); //const version will be called
vector<int> myvector;    
vector<int>::const_iterator it = myvector.begin(); //non-const version will be called

From C++ standard (§13.3.1 Candidate functions and argument lists):

For non-static member functions, the type of the implicit object parameter is “reference to cv X” where X is the class of which the function is a member and cv is the cv-qualification on the member function declaration. [Example: for a const member function of class X, the extra parameter is assumed to have type “reference to const X”. ]

So, in your case, if myvector object is const compiler will pick version of begin which has implicit object parameter of type reference to const vector which is const version of begin.

The compiler determines if the object variable is const or not at compile time

It then picks the corresponding overload, and whatever return type it has.

class C {
    public:
        int f() { return 1; }
        float f() const { return 1.5; }
// Non const.
assert(c.f() == 1);
// Convert variable const at compile time.
assert(const_cast<const C&>(c).f() == 1.5);
// Same as above but with an explicit reference.
const C& d = c;
assert(d.f() == 1.5);
// Analogous but with a new const object from the start.
const C e;
assert(d.f() == 1.5);
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.