0

I was a bit confused by the fact that the functions differ in presence/absence cv-qualifiers are equiavalent N4296::13.1/3.4 [over.load]:

Parameter declarations that differ only in the presence or absence of const and/or volatile are equivalent.

Example:

#include <iostream>

void foo(int){ }

void foo(const int){ } //error: redifinition

int main(){ }

DEMO

Now, let me provide an example with member-functions.

#include <iostream>

struct A
{
    A(){ }
    void foo(){ std::cout << "foo()" << std::endl; }
    void foo() const{ std::cout << "foo() const" << std::endl; }
};

A aa;
const A a;
int main(){ aa.foo(); a.foo(); }

DEMO

N4296::13.3.1/2 [over.match.funcs]

member function is considered to have an extra parameter, called the implicit object parameter, which represents the object for which the member function has been called

So, the member function declarations are different only in presence of the const-qualifier, but they are still overloadable. Doesn't it contradict to the quote from N4296::13.1/3.4 [over.load] I provided before?

4
  • There is huge difference.Const member function means you can't modify data in that function. Commented Jan 5, 2015 at 5:33
  • No, the "constness" of a member function is part of the function signature. Commented Jan 5, 2015 at 5:33
  • Why do you think the C++ rules are applicable to the things on asm level / in the compiler (implicit object pointer)? Commented Jan 5, 2015 at 5:35
  • The difference between A* and const A* is very different from the difference between int and const int. Commented Jan 5, 2015 at 9:40

2 Answers 2

2

The quoted passage (disclaimer: I haven't checked the quote or attribution, but anyway, this property of C++) is about top level const (and volatile) for a formal argument.

For example,

void foo( const int x );

is equivalent to

void foo( int x );

with respect to calling, checking its type, and so on. This is because there is no way it can matter to a caller whether a formal argument is const or not. That const-ness is only a restriction on what the function itself can do, not what callers can do.

And so for void foo( int ) you can provide an implementation with const:

void foo( const int x ) { cout << x << endl; }

It implements void foo( int ) because they're equivalent.

With a const member function you're instead saying that the referent for this is const. That's not a top level const. Adding that const is roughly equivalent to changing

void bar( int* p );

into

void bar( const int* p );

which are two different functions.

Sign up to request clarification or add additional context in comments.

2 Comments

Did you mean the implicit argument parameter has a pointer to type?
@DmitryFucintv: Essentially, yes, in the sense that we can imagine a rewrite of dot notation calls to calls with the implicit argument present as an explicit first argument. To make that view work one would have to consider that this is formally an rvalue. But essentially.
0

There is huge difference.Const member function means you can't modify data in that function.It includes in function signature and thats how compiler differentiates.Also accroding to C++ FAQ

The trailing const on foo() member function means that the abstract (client-visible) state of the object isn't going to change. This is slightly different from promising that the "raw bits" of the object's struct aren't going to change. C++ compilers aren't allowed to take the "bitwise" interpretation unless they can solve the aliasing problem, which normally can't be solved (i.e., a non-const alias could exist which could modify the state of the object).

    #include <iostream>

    struct A
    {
        A(){var=0; }
        void foo(){ std::cout << "foo()" << std::endl; }
        void foo() const{ std::cout << "foo() const" << std::endl;var=5; } //this will generate error

        private:
        int var;
    };



A aa;
const A a;
int main(){ aa.foo(); a.foo(); }

we will get error: read-only variable is not assignable

see this http://coliru.stacked-crooked.com/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.