Home > Back-end >  Is there no such thing as "implicit this parameter" in the Standard?
Is there no such thing as "implicit this parameter" in the Standard?

Time:04-18

Recently, I asked this question where one of the answers says:

There's no such thing as "implicit this parameter" in the standard. The standard calls it an "implicit object parameter".

Then someone commented that:

There's no such thing as "implicit this parameter" in the standard." seems wrong. From expr.call#4: "If the function is a non-static member function, the this parameter of the function shall be initialized with a pointer to the object of the call, converted as if by an explicit type conversion."

Seeing the above comment i think that the answer is technically incorrect because the answer said that "There's no such thing as "implicit this parameter" in the standard." while the standard clearly talks about the this parameter.

So how to interpret this further (pun intended)? I mean, it seems that the standard makes a distinction between the non-static member function and a constructor in the context of this parameter. For example, the standard says that for a non-static member function, the this parameter of the function shall be initialized with a pointer to the object of the call converted as if by an explicit type conversion. But the standard doesn't say the same for constructors. So why does the standard makes this distinction? I mean why doesn't the standard says that constructors also have an this parameter that get initialized by the passed argument just like for non-static member functions. This again leads to the deeper question that if there is no this parameter in the constructor unlike non-static member function, then how are we able to use this inside the constructor. For example, we know that we can write this->p = 0 inside the constructor as well as inside a non-static member function, where p is a data member. But in case of non-static member function, this is a parameter of that particular member function so this->p makes sense. But in case of constructor this is not a parameter, so how are we able to use this->p inside the constructor.

Originally, by reading the answers here, I thought that the implicit this parameter is an implementation detail. But after reading expr.call#4 it seems that it is not an implementation detail.

CodePudding user response:

If you think this is some sort of implicit parameter, type in this code:

#include <iostream>

struct SimpleThing {
    int xyzzy;
    SimpleThing(): xyzzy(42) {}
    void print(int plugh, const int twisty) {
        std::cout << xyzzy << '\n';
        std::cout << plugh << '\n';
        std::cout << twisty << '\n';
        xyzzy = 0;
        plugh = 0;
        twisty = 0;
        this = 0;
    }
};

int main() {
    SimpleThing thing;
    thing.print(7, 99);
}

Then examine the errors you get:

prog.cpp: In member function ‘void SimpleThing::print(int, int)’:
prog.cpp:12:16: error: assignment of read-only parameter ‘twisty’
   12 |         twisty = 0;
      |         ~~~~~~~^~~
prog.cpp:13:16: error: lvalue required as left operand of assignment
   13 |         this = 0;
      |                ^

Note that the first two assignments work because they are modifiable variables. The third fails because it is, of course, (non-modifiable) const.

The attempted assignment to this doesn't look like any sort of "can't write to some sort of variable" diagnostic because it actually isn't.

The this keyword is a special marker inside member functions that is translated into the address of the object being worked upon. While it may be passed as a hidden parameter, that is very much an implementation detail with which the standard does not concern itself with.

The controlling section in the C 20 standard is [class.this]:

In the body of a non-static (10.3.1) member function, the keyword this is a prvalue whose value is the address of the object for which the function is called.

Nowhere in there (the entire section) does it mention that this is some sort of hidden parameter to the call.


And, regarding your question on why there is no (cv-qualified) conversion for constructors like there are for other member functions, I believe that's because you can't actually create a cv-qualified constructor - it would be rather useless if your constructor were not allowed to set any member variables, for example :-)

While constructors can be used to create const/volatile objects, the constructor itself is not cv-qualified. This is covered at the end of the afore-mentioned section:

Constructors (10.3.4) and destructors (10.3.7) shall not be declared const, volatile or const volatile. [Note: However, these functions can be invoked to create and destroy objects with cv-qualified types, see 10.3.4 and 10.3.7. — end note]

CodePudding user response:

Here's a quotation from this Draft C 17 Standard (bolding for emphasis, and to answer the question, is mine):

10.3.3 The using declaration      [namespace.udecl]



16     For the purpose of forming a set of candidates during overload resolution, the functions that are introduced by a using-declaration into a derived class are treated as though they were members of the derived class. In particular, the implicit this parameter shall be treated as if it were a pointer to the derived class rather than to the base class. This has no effect on the type of the function, and in all other respects the function remains a member of the base class. Likewise, constructors that are introduced by a using-declaration are treated as though they were constructors of the derived class when looking up the constructors of the derived class …

However I should add that the cited paragraph doesn't seem to be present in this later Draft Standard. In fact, that (later) Standard seems to use the phrase, "implicit object parameter," in similar clauses.

So, maybe you should add a specific version tag to your question: or , as there appears to be a divergence in the use (or not) of the term.


Note that the above citation is the only occurrence of the phrase, "implicit this parameter" in that Draft Standard.


Also, note that both documents I have linked are only Draft versions of the respective Standards, and both come with this cautionary escape-clause:

Note: this is an early draft. It’s known to be incomplet and incorrekt, and it has lots of bad formatting.

  • Related