c++ - pointer to member function of incomplete type -


i don't understand why adding forward declaration class changes size of pointer member type

#include <iostream> using namespace std;  int main() {     //struct cl;     //cout<<sizeof(int (cl::*)())<<endl;      struct cl{};     cout<<sizeof(int (cl::*)())<<endl; } 

output vs2013:
4

but if uncomment first 2 lines in main(), output different:
16
16

so, simple adding forward declaration before definition of struct cl increases size of pointer member of cl. why? know size of member function pointer depends structure of type (e.g. virtual functions , base classes may increase it), why can sizeof operator applied pointer member of incomplete type? or can't? have not found in standard

the msvc compiler uses different sizes pointers member functions optimization. optimization violates standard. kudos igor tandetnik mentioning reinterpret_cast in a msdn form post, [expr.reinterpret.cast]p10

a prvalue of type “pointer member of x of type t1” can explicitly converted prvalue of different type “pointer member of y of type t2” if t1 , t2 both function types or both object types. null member pointer value converted null member pointer value of destination type. result of conversion unspecified, except in following cases:

  • converting prvalue of type “pointer member function” different pointer member function type , original type yields original pointer member value.

so there's roundtrip guarantee, forces conforming implementations use same size pointer member function types.


the msvc optimization performed if /vmb switch set. case of single inheritance, optimised pointer member function requires void*-sized storage, see the old new thing: pointers member functions strange animals.

if forward-declare type cl , form pointer-to-member function, optimization deactivated (i not find documentation on that, unfortunately). otherwise, might inconsistent sizes before , after definition of cl.

by way, can inconsistent sizes enumerations in vs2010, if forward-declare them without specifying underlying type , later explicitly define underlying type definition of enum. works language extensions activated.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -