Sizeof a member function pointer of an incomplete type messes another sizeof after the type has been defined - by tailsu

Status : 

  By Design<br /><br />
		The product team believes this item works according to its intended design.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.


0
0
Sign in
to vote
ID 338226 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 4/15/2008 1:59:22 AM
Access Restriction Public

Description

Use the following code to reproduce the bug. Note that if the first sizeof is removed, the code compiles cleanly.

struct bar;
int a = sizeof(void (bar::*)());

struct bar
{
	void foo();
};
char x[sizeof(&bar::foo) == 16 ? -1 : 1];
Sign in to post a comment.
Posted by Microsoft on 4/18/2008 at 9:45 AM
Hi: this is By-Design. By having that first sizeof you are forcing the compiler to decide what the pointer-to-member representation for the class bar is. As the class bar is not yet defined the compiler has to choose the most pessimistic (i.e. largest) representation for the pointer-to-member. Once it has choosen a representation it sticks with it -- even it it turns out that when that the definition of bar is such that it could have used a more optimial representation. Do avoid this you need to give the compiler more information - something like:

struct __single_inheritance bar;
int a = sizeof(void (bar::*)());

struct bar
{
    void foo();
};

char x[sizeof(&bar::foo) == 16 ? -1 : 1];

Jonathan Caves
Visual C++ Compiler Team

Posted by Microsoft on 4/15/2008 at 8:34 PM
Thanks for your feedback.

We are escalating this issue to the appropriate group within the Visual Studio Product Team for triage and resolution.
These specialized experts will follow-up with your issue.

Thank you,
Visual Studio Product Team