|
Boost : |
From: Corrado Zoccolo (czoccolo_at_[hidden])
Date: 2007-10-22 04:37:09
On 10/22/07, Mathias Gaunard <mathias.gaunard_at_[hidden]> wrote:
>
> Corrado Zoccolo wrote:
>
> > BTW, the idea of the hand made vtable born in the discussion with gpd.
> My
> > first implementation used just one function ptr to encapsulate all the
> > behaviour (with a signature to fit all the methods), and was
> substantially
> > less efficient than the current solution.
>
> Instead of a hand make vtable, why not this?
>
> struct impl_base
> {
> virtual void something(void*) = 0;
> };
>
> template<typename T>
> struct impl
> {
> void something(void* p)
> {
> // do something useful
> }
> };
>
> template<typename T>
> class poly
> {
> template<typename U>
> poly(const U& u)
> {
> new(&type_info) impl<U>;
> ...
> }
>
> typename boost::aligned_storage<
> sizeof(impl<T>),
> boost::alignment_of< impl<T> >
> >::type type_info;
> };
>
> This simply assumes that all instances of impl have the same size and
> alignment.
Because the vtables in C++ are not smart enough. If I make an hand made
vtable, I can put not only function pointers, but also values inside it.
This means that for example, I have to make a call through a function
pointer to query if the object was allocated on heap or on stack, or if it
has a no-throw copy constructor (all things that I can put directly in my
hand made vtable).
I think a valuable addition to c++0x would be "virtual constexpr" methods,
i.e. virtual methods that are guaranteed to return compile time constants,
but different classes in the hierarchy can return different values. This
allows for a big optimization, i.e. the compiler can store the value of the
constant into the vtable, and save an unpredictable call through a pointer.
Corrado
_______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>
-- __________________________________________________________________________ dott. Corrado Zoccolo mailto:zoccolo_at_[hidden] PhD - Department of Computer Science - University of Pisa, Italy -------------------------------------------------------------------------- The self-confidence of a warrior is not the self-confidence of the average man. The average man seeks certainty in the eyes of the onlooker and calls that self-confidence. The warrior seeks impeccability in his own eyes and calls that humbleness. Tales of Power - C. Castaneda
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk