Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong type of "this" pointer detected in a non-const method #2303

Closed
Leedehai opened this issue Jul 21, 2018 · 5 comments
Closed

Wrong type of "this" pointer detected in a non-const method #2303

Leedehai opened this issue Jul 21, 2018 · 5 comments
Labels
bug fixed Check the Milestone for the release in which the fix is or will be available. Language Service Visual Studio Inherited from Visual Studio
Milestone

Comments

@Leedehai
Copy link

Leedehai commented Jul 21, 2018

Type: LanguageService

Describe the bug

  • OS and Version: macOS High Sierra 10.13.5
  • VS Code Version: 1.25.1
  • C/C++ Extension Version: 0.17.7
  • Other extensions you installed (and if the issue persists after disabling them): only this extension pertains to C/C++.
  • A clear and concise description of what the bug is: In a non-const, no-volatile method of class MyClass, the this pointer should be of type MyClass * in the hovering message (per C++11 standard 9.3.2 paragraph 1, C++17 standard 12.2.2.1 paragraph 1), not MyClass *const.

To Reproduce
Steps to reproduce the behavior:

  1. Go to VSCode, open a fresh new file test.cc.
  2. Type in the code below.
class MyClass {
public:
	MyClass *getPointer() { return this; }
};
int main() {}
  1. Save it.
  2. Hover above the this pointer, examine the message.
  3. The message says it's MyClass *const this.

Expected behavior
The type message should be MyClass *this.

Screenshots
1081532133048_ pic

Additional text: this StackOverflow post said in the days of C++98 and C++03 old GCC and MSVC compilers internally used a trick that labels the this pointer as a const pointer. That trick was benign at that time but became problematic in C++11 and later standards. GCC has since abandoned that trick. I'm wondering if this is the problem. That post also said as of 2017 MSVC didn't fix the problem.

@sean-mcmanus
Copy link
Contributor

I believe this is "by design" and repros in VS 2017. The type "MyClass *const" is correct for "this" and it means "this" cannot be assigned to (it's const) and it points to an object of type "MyClass *", which is not const. If getPointer() was defined as "MyClass *getPointer() const" then the type of "this" would be "const MyClass *const".

@Leedehai
Copy link
Author

Leedehai commented Jul 21, 2018

@sean-mcmanus But it violates the C++11 standard :( so there is currently no plan in the VS team to be more aligned with the standard on this issue? And, does VSCode's cpptool use VS as its internal engine?

@sean-mcmanus
Copy link
Contributor

sean-mcmanus commented Jul 21, 2018

Okay, it looks like you're right, it's a bug: https://stackoverflow.com/questions/6067244/type-of-this-pointer .

It got reported to VS: https://developercommunity.visualstudio.com/content/problem/293655/class-foo-const-this-reported-while-in-not-const-c.html . If they fix it, we'll take the fix as well.

@sean-mcmanus
Copy link
Contributor

sean-mcmanus commented Dec 28, 2018

VS "Won't Fixed" the bug saying it didn't have enough "votes", but when I tried to upvote it, it wouldn't let me, so I created a new issue at https://developercommunity.visualstudio.com/content/problem/416906/c-intellisense-wrong-type-of-this-pointer-detected.html -- that issue will probably need to get enough upvotes in order to get fixed though.

@sean-mcmanus sean-mcmanus added Visual Studio Inherited from Visual Studio and removed parser labels Feb 13, 2019
@sean-mcmanus sean-mcmanus added the fixed Check the Milestone for the release in which the fix is or will be available. label Aug 13, 2019
@sean-mcmanus sean-mcmanus added this to the 0.26.0 milestone Aug 13, 2019
@sean-mcmanus sean-mcmanus modified the milestones: 0.26.0, 0.25.0 Aug 20, 2019
@sean-mcmanus
Copy link
Contributor

Fixed with 0.25.0.

@github-actions github-actions bot locked and limited conversation to collaborators Oct 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug fixed Check the Milestone for the release in which the fix is or will be available. Language Service Visual Studio Inherited from Visual Studio
Projects
None yet
Development

No branches or pull requests

3 participants