-1

Edit: Please read the question. The suggested duplicates don't answer my question. (I already read those answers before posting this question.)


If compiling the code below, MSVC spits out a quite well-known error message cannot convert 'this' pointer from 'const S' to 'S &'.

We can remove the error by modifying the S::f into a const function, but my question is why the message is like that.

I thought the error message should be: cannot convert 'this' pointer from 'const S*' to 'S*'. Why it's 'const S' to 'S &', not 'const S*' to 'S*'?

(I read some answers such as this and this, just for the context of my curiosity.)

struct S {
    void f(){}
};
int main() {
    const S s;
    s.f(); // 'void S::f(void)': cannot convert 'this' pointer from 'const S' to 'S &' 
}
starriet
  • 2,565
  • 22
  • 23
  • `int *p;` You say p is a pointer to `int`, you don't say it is a pointer to `int*`. So the phrase is correct. IDK why they use a reference. Better to ask directly https://learn.microsoft.com/en-us/answers/questions/ask/ – 273K Apr 16 '23 at 03:14
  • 3
    *"my question is why the message is like that..."* This should be asked at msvc's official site. Stackoverflow is not the right place for seeking reasons why msvc uses potentially incorrect descirption of the error. You already seem to know that there should be no reference mentioned in the error message. – Jason Apr 16 '23 at 03:46
  • Just sloppy wording on Microsoft's part. – n. m. could be an AI Apr 16 '23 at 17:38
  • @jason So, do you think the error message is incorrect and there should be no `&` in the message(`S`, not `S&`)? Then what about the `*`? Let me know your opinion and that's why I asked this question. As a C++ beginner, I was just curious if I missed some concepts and didn't interpret the error message appropriately. I thought Stack Overflow is the place where learners can ask questions when they are not sure if they're doing things right... no? :) Anyway, thanks for your advice! – starriet Apr 17 '23 at 12:50

1 Answers1

3
struct S {
    void f(){}
};

The f method gets a default specifier &:

struct S {
    void f() & {}
};

Other non default specifiers are &&, const (that is const&) and const&&.

The method can be considered as a function that takes an object as a first parameter void f(S&); that accepts S& but gets const S, thus the error is pretty clear: cannot convert ... from 'const S' to 'S &'. Why 'this' pointer still is a question.

273K
  • 29,503
  • 10
  • 41
  • 64
  • Thank you. I'm new to this concept, though. Are you talking about the _"member functions with ref-qualifier"_ in [this page](https://en.cppreference.com/w/cpp/language/member_functions)? Also, I thought typical member functions take `this` as a first (hidden) parameter, doesn't it? Do you mean `this` is `S&`? – starriet Apr 17 '23 at 12:39
  • Yes, the link is correct. `this` has a type `S*` or `const S*`. Compilers don't deal with object names, they deal with addresses. If you look at Assembler, a reference function parameter `T& arg` or `const T& arg` takes an address of a passed object, so a function Assembler deals with a pointer `T* arg`. – 273K Apr 17 '23 at 15:52