[TYPES] Any formal models of method lookup mechanism in C++?

DeLesley Hutchins delesley at gmail.com
Mon Jun 26 13:10:52 EDT 2017


I'm not aware of any existing models; formalizing C++ in its entirety is
borderline-hopeless.

Method lookup in C++ is extremely complicated.  In the simple case, it
depends on the name of the method, the number and types of arguments
(including const), any visible type implicit type conversions, and whether
arguments can be considered l-value or r-value references.  Once you add
templates and operator overloading, search can expand to the namespaces in
which the argument types are declared, and you have to take template
specializations and various type equivalence and pattern matching rules
into account.  Inheriting from a template also disables many forms of
lookup. Formalizing all that will be a real challenge.

However, all of that complexity is on the static side, and the end result
is that the compiler selects a method declaration which, for linking
purposes, must have a unique mangled name.  In a formal model, you could
potentially rely on the mangled name to identify methods, and assume that
all template instantiation has already occurred, which would bypass most of
the mess.  Virtual methods cannot be templatized.  The dynamic dispatch is
comparatively trivial, being simple single-dispatch with manual resolution
of any MI ambiguities; your main challenge will be formalizing the
virtual/non-virtual inheritance distinction.


On Mon, Jun 26, 2017 at 7:51 AM, Yanlin Wang <huohuohuomumu at gmail.com>
wrote:

> [ The Types Forum, http://lists.seas.upenn.edu/mailman/listinfo/types-list
> ]
>
>   Dear all,
>
>
> I am formalizing a C++-like multiple inheritance model, please look at
> this piece of code:
>
>
> //g++  5.4.0#include <iostream>class Deck {    public:        virtual void
> draw () {
>             std::cout << "Deck::draw" << std::endl;
>         }        virtual void shuffleAndDraw () {
>             std::cout << "Deck::shuffle" << std::endl; draw();
> }};class LDeck : public Deck {    public:
>         virtual void draw () {
>             std::cout << "L::draw" << std::endl;
>         }};class Paint {    public:
>         virtual void draw () {
>             std::cout << "Paint::draw" << std::endl;
>         }};class Top : public LDeck, public Paint {};int main(){    Top
> b;    Top *a = &b;    a->shuffleAndDraw();//shuffle and Ldraw
> a->Deck::draw(); //draw    a->Paint::draw();//paint
> ((Deck*)a)->draw();//LDraw    ((Paint*)a)->draw();//paint    return 0;}
>
>
>
>
> For resolving the method call “ ((Deck*)a)->draw()”, C++ uses both the
> static (Deck) and dynamic(Top) type information of object a, so that the
> method call “draw()” dispatches to LDeck::draw().
>
>
> I was wondering whether there are any formal models that describes this
> method lookup mechanism in C++. If anyone knows, could you please let me
> know? Thank you!
>
>
> Best regards,
> Yanlin Wang
>
>
>
>


More information about the Types-list mailing list