Topic: Dynamic multimethod/variadic-type-cast container.


Author: Chris Gary <cgary512@gmail.com>
Date: Wed, 11 Feb 2015 02:01:15 -0800 (PST)
Raw View
------=_Part_1686_621816740.1423648875258
Content-Type: multipart/alternative;
 boundary="----=_Part_1687_1218522878.1423648875258"

------=_Part_1687_1218522878.1423648875258
Content-Type: text/plain; charset=UTF-8

Before I venture to write something up, I wanted to run this by everyone.

I posted this before, but I wasn't happy with the title, formatting, lack
of clarity in some areas, and some mistakes.

*Scenario:*

A single, extensible descriptive layer wherein types describe only part of
an object's state.

Several concrete implementations that must extend, at runtime, objects of
descriptive types with relevant state (e.g. A graphics library where "image"
maps to "OpenGLTexture", "Direct3DTexture", or "GDIBitmap" opaquely).

Instances of descriptive types can be re-attached to different
implementations at runtime (e.g. switching renderers without re-loading
active resources from disk). Ergo, straightforward inheritance and
shared_ptr will not work.

A concrete implementation should also provide "best-fit" support for
user-defined types derived from those in the descriptive layer. It should
be possible to alter this support at runtime. Different instances of
implementations (e.g. different graphics context instances) may have
different support depending on plugin/extension selection (or anything,
really).

In other words: Extensible best-fit PIMPL.

*Solution:*

We need a type map! Not just a static type switch, but an unordered_map
analogue with the semantics of dynamic_cast over the set of target types.

Similar to multimethods, except variations are specified independently and
managed by containers at runtime (this is convenient for dynamic
plugin-based architectures).

*Definition Mockup:*

    // Note: Omitting allocator details and
    // assuming polymorphic types for now.
    //
    template<typename SignatureType>
    class caster;

    template<
        typename ResultType,
        typename ArgType, typename ...ArgTypes
    >
    class caster<ResultType(ArgType, ArgTypes...)>
    {
    public:

        using signature = ResultType(ArgType, ArgTypes...);

        template<typename ...OtherArgTypes>
        enable_if<is_polymorphically_covariant<
            ResultType(OtherArgTypes...),
            signature
        >, void> erase()    // NO ARGUMENTS
        {
            // Lookup dispatch entry
                // Erase if it exists
        }

        template<typename HandlerType>
        enable_if<is_polymorphically_covariant<
            signature_of<decay<HandlerType>>, signature
        >, void> to(HandlerType &&handler)
        {
            // Lookup dispatch entry
                // If it exists
                    // Throw bad_argument or something

            // Allocate handler node, etc...
        }

        ResultType operator()(ArgType arg, ArgTypes...args)
        {
            // Note: Timing of key construction depends on
            // the complexity of the inheritance hierarchy
            // from which the cast starts searching.
            //
            // Lookup is always O(1) on average.

            // This is unambiguous if matches are given
            // preference in left-to-right order.

            // In the monadic case, this is always unambiguous
            // (somewhat like an open dynamic_cast;
            // the first match wins).

            // The casting logic obviously requires runtime support,
            // and is somewhat slow in the variadic case due to
            // ABI limitations.

            // Lookup best-fitting dispatch entry
            // (first by vptr memos, then table lookup)
                // If it exists
                    // Invoke and return result
                // Else
                    // Throw bad_cast
        }

        template<
            typename HandlerType,
            typename ...HanderTypes,
            typename Enabler = enable_if<
                conjunction<
                    is_polymorphically_covariant<
                        signature_of<decay<HandlerType>>, signature
                        >,
                    is_polymorphically_covariant<
                        signature_of<decay<HanderTypes>>, signature
                        >...
                >, void>
            >
        >
        caster(HandlerType &&handler, HanderTypes &&...handlers)
        {
            // Fill table.

            // Note: We can do static collision
            // checking here!
        }

        caster &operator = (const caster &other)
        {// Copy contents
        }
        caster &operator = (caster &&other)
        {// Clear and move other
        }

        caster(){}
        caster(const caster &other){/*Copy contents*/}
        caster(caster &&other){/*Direct move*/}
        ~caster() noexcept
        {}
    };

*Usage Mockup (monadic):*

    class base{public:virtual ~base(){}};
    class derived : public base{};
    class left_derived : virtual public derived{};
    class right_derived : virtual public derived{};
    class most_derived : public left_derived, public right_derived{};

    caster<int(base &)> c{
        [](derived &){          // CASE 1
            return 1;
        },
        [](right_derived &){        // CASE 2
            return 2;
        },
        [](const left_derived &){    // CASE 3
            return 3;
        }
    };

    base b;
    derived d;
    left_derived ld;
    right_derived rd;
    most_derived md;

    c(b);    // bad_cast (nothing can accept only a "base &")
    c(d);    // calls CASE 1
    c(ld);    // calls CASE 3
    c(md);    // calls CASE 3 (left_derived matched first during BFS)
    c(rd);    // calls CASE 2

    c.to([](base &){    // CASE 4
        return 4;
    });
    c.to([](const base &){    // bad_argument: Entry already exists
        return 4;
    });

    c(b);    // calls CASE 4

    c.erase<left_derived &>();
    // erases CASE 3

    c.erase<const left_derived &>();
    // does nothing
    // Good? Bad?
    // I don't like the idea of iterators here...

    promise<base &> p;

    // Note: This assumes "then" can accept a
    // callable with a simple value-type signature.
    //
    future<int> f = p.get_future().then(move(c));

    p.set_value(md);    // calls CASE 2 (right_derived matched)

    auto y = f.get();    // y = 2

I have a prototype implementation, but I'm not able to publish the source
at the moment. I will put something together if there is sufficient
interest.


--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

------=_Part_1687_1218522878.1423648875258
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Before I venture to write something up, I wanted to run th=
is by everyone.<br><br>I posted this before, but I wasn't happy with the ti=
tle, formatting, lack of clarity in some areas, and some mistakes.<br><br><=
b>Scenario:</b><br><br>A single, extensible descriptive layer wherein types=
 describe only part of an object's state.<br><br>Several concrete implement=
ations that must extend, at runtime, objects of descriptive types with rele=
vant state (e.g. A graphics library where "<span style=3D"font-family: cour=
ier new,monospace;">image</span>" maps to "<span style=3D"font-family: cour=
ier new,monospace;">OpenGLTexture</span>", "<span style=3D"font-family: cou=
rier new,monospace;">Direct3DTexture</span>", or "<span style=3D"font-famil=
y: courier new,monospace;">GDIBitmap</span>" opaquely).<br><br>Instances of=
 descriptive types can be re-attached to different implementations at runti=
me (e.g. switching renderers without re-loading active resources from disk)=
.. Ergo, straightforward inheritance and <span style=3D"font-family: courier=
 new,monospace;">shared_ptr</span> will not work.<br><br>A concrete impleme=
ntation should also provide "best-fit" support for user-defined types deriv=
ed from those in the descriptive layer. It should be possible to alter this=
 support at runtime. Different instances of implementations (e.g. different=
 graphics context instances) may have different support depending on plugin=
/extension selection (or anything, really).<br><br>In other words: Extensib=
le best-fit PIMPL.<br><br><b>Solution:</b><br><br>We need a type map! Not j=
ust a static type switch, but an <span style=3D"font-family: courier new,mo=
nospace;">unordered_map</span> analogue with the semantics of <span style=
=3D"font-family: courier new,monospace;">dynamic_cast</span> over the set o=
f target types.<br><br>Similar to multimethods, except variations are speci=
fied independently and managed by containers at runtime (this is convenient=
 for dynamic plugin-based architectures).<br><br><b>Definition Mockup:</b><=
br><br><span style=3D"font-family: courier new,monospace;">&nbsp;&nbsp;&nbs=
p; // Note: Omitting allocator details and<br>&nbsp;&nbsp;&nbsp; // assumin=
g polymorphic types for now.<br>&nbsp;&nbsp;&nbsp; //<br>&nbsp;&nbsp;&nbsp;=
 template&lt;typename SignatureType&gt;<br>&nbsp;&nbsp;&nbsp; class caster;=
<br>&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp; template&lt;<br>&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp; typename ResultType,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp; typename ArgType, typename ...ArgTypes<br>&nbsp;&nbsp;&nbsp=
; &gt;<br>&nbsp;&nbsp;&nbsp; class caster&lt;ResultType(ArgType, ArgTypes..=
..)&gt;<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp; public:<br>&nbsp; &nbs=
p;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; using signature =3D Result=
Type(ArgType, ArgTypes...);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br>&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; template&lt;typename ...OtherArgTyp=
es&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; enable_if&lt;is_polymo=
rphically_covariant&lt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp; ResultType(OtherArgTypes...),<br>&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; signature<br>&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp; &gt;, void&gt; erase()&nbsp;&nbsp;&nbsp; // NO ARG=
UMENTS<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Lookup dispatch entry<=
br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp; // Erase if it exists<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; template&lt=
;typename HandlerType&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ena=
ble_if&lt;is_polymorphically_covariant&lt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; signature_of&lt;decay&lt;HandlerType&=
gt;&gt;, signature<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &gt;, void=
&gt; to(HandlerType &amp;&amp;handler)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp; // Lookup dispatch entry<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // If it exists<br>&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Throw bad_argument or something<br>&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp; // Allocate handler node, etc...<br>&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp; ResultType operator()(ArgType arg, ArgTypes...args)<br>&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp; // Note: Timing of key construction depends on=
<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;// the complex=
ity of the inheritance hierarchy<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&=
nbsp;&nbsp; &nbsp;// from which the cast starts searching.<br>&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;//<br>&nbsp;&nbsp; &nbsp;&nbsp;=
&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;// Lookup is always O(1) on average.<br><br=
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // This=
 is unambiguous if matches are given<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // preference in left-to-right order.<br>&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // In the monadic ca=
se, this is always unambiguous<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp; // (somewhat like an open dynamic_cast;<br>&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // the first m=
atch wins).<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp; // The casting logic obviously requires runtime support,<br>&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // and is so=
mewhat slow in the variadic case due to<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // ABI limitations.<br><br>&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Lookup best-fittin=
g dispatch entry<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp; // (first by vptr memos, then table lookup)<br>&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
// If it exists<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Invoke and re=
turn result<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp; // Throw bad_cast<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br=
><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; template&lt;<br>&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; typename HandlerTyp=
e,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ty=
pename ...HanderTypes,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp; typename Enabler =3D enable_if&lt;<br>&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; con=
junction&lt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; is_polymorphically_=
covariant&lt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp; signature_of&lt;decay&lt;HandlerType&gt;&gt;, signature<br>&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &gt;,<br>&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp; is_polymorphically_covariant&lt;<br>&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; signature_of&lt;decay&=
lt;HanderTypes&gt;&gt;, signature<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp; &gt;...<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &gt;, void&gt;<br>&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &gt;<br>&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp; caster(HandlerType &amp;&amp;handler, HanderTypes &amp;&amp;=
....handlers)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Fill table.<br>&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Note: We can do =
static collision<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp; // checking here!<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
 }<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; caster &amp;operator =
=3D (const caster &amp;other)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
 {// Copy contents<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; caster &amp;operator =3D (caster &amp=
;&amp;other)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {// Clear and mo=
ve other<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; caster=
(){}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; caster(const caster &amp=
;other){/*Copy contents*/}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ca=
ster(caster &amp;&amp;other){/*Direct move*/}<br>&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp; ~caster() noexcept<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp; {}<br>&nbsp;&nbsp;&nbsp; };</span><br>&nbsp; &nbsp;<br><b>Usage Mock=
up (monadic):</b><br><br><span style=3D"font-family: courier new,monospace;=
">&nbsp;&nbsp;&nbsp; class base{public:virtual ~base(){}};<br>&nbsp;&nbsp;&=
nbsp; class derived : public base{};<br>&nbsp;&nbsp;&nbsp; class left_deriv=
ed : virtual public derived{};<br>&nbsp;&nbsp;&nbsp; class right_derived : =
virtual public derived{};<br>&nbsp;&nbsp;&nbsp; class most_derived : public=
 left_derived, public right_derived{};<br><br>&nbsp;&nbsp;&nbsp; caster&lt;=
int(base &amp;)&gt; c{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [](der=
ived &amp;){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // CASE =
1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ret=
urn 1;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; },<br>&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp; [](right_derived &amp;){&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp; // CASE 2<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp; return 2;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp; },<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [](const left_derived=
 &amp;){&nbsp;&nbsp;&nbsp; // CASE 3<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 3;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; };<br><br>&nbsp;&nbsp;&nbsp; base b;<b=
r>&nbsp;&nbsp;&nbsp; derived d;<br>&nbsp;&nbsp;&nbsp; left_derived ld;<br>&=
nbsp;&nbsp;&nbsp; right_derived rd;<br>&nbsp;&nbsp;&nbsp; most_derived md;<=
br><br>&nbsp;&nbsp;&nbsp; c(b);&nbsp;&nbsp;&nbsp; // bad_cast (nothing can =
accept only a "base &amp;")<br>&nbsp;&nbsp;&nbsp; c(d);&nbsp;&nbsp;&nbsp; /=
/ calls CASE 1<br>&nbsp;&nbsp;&nbsp; c(ld);&nbsp;&nbsp;&nbsp; // calls CASE=
 3<br>&nbsp;&nbsp;&nbsp; c(md);&nbsp;&nbsp;&nbsp; // calls CASE 3 (left_der=
ived matched first during BFS)<br>&nbsp;&nbsp;&nbsp; c(rd);&nbsp;&nbsp;&nbs=
p; // calls CASE 2<br>&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp; c.to([](base &amp=
;){&nbsp;&nbsp;&nbsp; // CASE 4<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p; return 4;<br>&nbsp;&nbsp;&nbsp; });<br>&nbsp;&nbsp;&nbsp; c.to([](const =
base &amp;){&nbsp;&nbsp;&nbsp; // bad_argument: Entry already exists<br>&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 4;<br>&nbsp;&nbsp;&nbsp; });=
<br><br>&nbsp;&nbsp;&nbsp; c(b);&nbsp;&nbsp;&nbsp; // calls CASE 4<br>&nbsp=
;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp; c.erase&lt;left_derived &amp;&gt;();<b=
r>&nbsp;&nbsp;&nbsp; // erases CASE 3<br><br>&nbsp;&nbsp;&nbsp; c.erase&lt;=
const left_derived &amp;&gt;();<br>&nbsp;&nbsp;&nbsp; // does nothing<br>&n=
bsp;&nbsp;&nbsp; // Good? Bad?<br>&nbsp;&nbsp;&nbsp; // I don't like the id=
ea of iterators here...<br><br>&nbsp;&nbsp;&nbsp; promise&lt;base &amp;&gt;=
 p;<br><br>&nbsp;&nbsp;&nbsp; // Note: This assumes "then" can accept a<br>=
&nbsp;&nbsp;&nbsp; // callable with a simple value-type signature.<br>&nbsp=
;&nbsp;&nbsp; //<br>&nbsp;&nbsp;&nbsp; future&lt;int&gt; f =3D p.get_future=
().then(move(c));<br><br>&nbsp;&nbsp;&nbsp; p.set_value(md);&nbsp;&nbsp;&nb=
sp; // calls CASE 2 (right_derived matched)<br><br>&nbsp;&nbsp;&nbsp; auto =
y =3D f.get();&nbsp;&nbsp;&nbsp; // y =3D 2</span><br><br>I have a prototyp=
e implementation, but I'm not able to publish the source at the moment. I w=
ill put something together if there is sufficient interest.<br>&nbsp;<br><b=
r></div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

------=_Part_1687_1218522878.1423648875258--
------=_Part_1686_621816740.1423648875258--

.


Author: Chris Gary <cgary512@gmail.com>
Date: Wed, 11 Feb 2015 02:36:32 -0800 (PST)
Raw View
------=_Part_4721_1345437545.1423650992528
Content-Type: multipart/alternative;
 boundary="----=_Part_4722_1913499552.1423650992528"

------=_Part_4722_1913499552.1423650992528
Content-Type: text/plain; charset=UTF-8

I would also like to add this is useful for things like message maps
(messages as types).

An interesting, and I think useful consequence of this is that polymorphic
messages must be substitutable.

I'm not a fan of integers, message ID enums, or string parsing when it
comes to extensibility.

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

------=_Part_4722_1913499552.1423650992528
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">I would also like to add this is useful for things like me=
ssage maps (messages as types).<br><br>An interesting, and I think useful c=
onsequence of this is that polymorphic messages must be substitutable.<br><=
br>I'm not a fan of integers, message ID enums, or string parsing when it c=
omes to extensibility.<br></div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

------=_Part_4722_1913499552.1423650992528--
------=_Part_4721_1345437545.1423650992528--

.