Note that there are some explanatory texts on larger screens.

plurals
  1. POPointer-to-member, type descriptors and references
    primarykey
    data
    text
    <p>I'm working on a type descriptor project in C++11. The type descriptor's job is to know the types of every member in a class, it's size and it's offset from the base of an object. I don't support multiple inheritance as well as objects with virtual methods so I am making this a lot simpler for now. The goal is to be able to serialize and unserialize objects using the descriptor.</p> <p><em>Note that this is a pet project to mess around with features like variadic templates, pointer to members and other features of C++ I am not familiar with, so there's no need to point me toward something like boost::archiving. :)</em></p> <p>The way I actually register members is very similar to boost::python::class_'s way.</p> <pre><code>ClassDescriptor fooDesc( "Foo" ); fooDesc.addMember( "a", &amp;Foo:: a ); fooDesc.addMember( "b", &amp;Foo:: b ); // (abridged for clarity) : template&lt; typename ClassType, typename MemberType &gt; ClassDescriptor&amp; ClassDescriptor::addMember( const char* name, MemberType ClassType::* member ) { return addMember&lt; MemberType &gt;( name, reinterpret_cast&lt; size_t &gt;( &amp;(((ClassType*)0)-&gt;*member)) ); } </code></pre> <p>Unfortunately, the pointer-to-member feature of C++ can't be used with references in C++, as I've learned earlier this week : <a href="https://stackoverflow.com/a/8336479/1074536">https://stackoverflow.com/a/8336479/1074536</a>, so I am not able to use &amp;Foo::refToAndInt for example.</p> <p>As far as how I am computing the offset of the member, I am not using the offset of macro since my classes will not always be PODs.</p> <p>So since I can't use pointer-to-members for computing the offset of references, I thought I'd try :</p> <pre><code>&amp;(((Foo*)nullptr)-&gt;refToAnInt) </code></pre> <p>but has it has been pointed out in another stack overflow thread, this is undefined behavior and on obviously on LLVM it crashes. :(</p> <p>I'd like to avoid doing stuff like taking the previous member's offset, adding it's size and then somehow computing the padding required to align my next member, since it would look messy and error prone</p> <p>So, I can't use both of those tricks and offsetof is only for PODs. Any suggestion on what I could try next, apart from my other horrible suggestion?</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload