C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Versatility -- Classes change at runtime

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Sun, 31 Jul 2022 12:40:09 +0200
Would all this be simply solved by some syntax sugar?
```

struct Foo
{
    using MebPtr = int (Foo::*)(int);

    int A(int);
    int B(int);

    static MebPtr StaticPointer;
    MebPtr Pointer = &Foo::A;
};
Foo::MebPtr Foo::StaticPointer = &Foo::A;

int main()
{
    Foo foo;

    (foo.*foo.StaticPointer)(10); // work now
    (foo.*foo.Pointer)(10);

    foo.Pointer(10); //error now, will be same as `(foo.*foo.Pointer)(10);`
    foo.Pointer = &Foo::B;
    foo.Pointer(10);

    foo.StaticPointer(10); //error now, will be same as
`(foo.*foo.StaticPointer)(10);`
    foo.StaticPointer = &Foo::B;
    foo.StaticPointer(10);
}
```
And you can make your own virtual table, that could be dynamic per
object or per type.



niedz., 31 lip 2022 o 12:03 Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> napisaƂ(a):
>
> On Sun, Jul 31, 2022 at 5:43 AM Thiago Macieira via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
> >>
> > > Start with a new syntax, because right now the vtables are in read-only
> > > memory. You must somehow signal the compiler that they should be modifiable.
> >
> > To be clear: you must signal in code.
> >
> > Compiler options are out of scope in this list. If you just want a compiler
> > extension, make a suggestion to the implementations you care about.
>
>
> Well then we should discuss whether this should change.
>
> Perhaps a very limited number of compiler options should be part of
> the C++ standard. Freedom of syntax could abide the following regex:
>
> (?:--|-|\/)cplusplus_([A-z0-9_]+)(?:(?:=|:)((?:\+|-)|(?:[:A-z0-9_]+(?:,[:A-z0-9_]+)*))?)
>
> The 1st capture group is the name of the option, and the 2nd capture
> group is what you've set it to. See this regex in action with examples
> here:
>
> https://regex101.com/r/PgCjUS/1
>
> This regex accepts the following three syntaxes:
>
> -cplusplus_polymorphic_class=::MyNamespace::MyClass,::OtherNamespace::OtherClass
> --cplusplus_polymorphic_class=::MyNamespace::MyClass,::OtherNamespace::OtherClass
> /cplusplus_polymorphic_class:::MyNamespace::MyClass,::OtherNamespace::OtherClass
>
> If you're willing to suffer the overhead of having all classes be
> polymorphic then do this:
>
> --cplusplus_polymorphic_class=+
>
> And if you want to turn the feature off completely then do:
>
> --cplusplus_polymorphic_class=-
>
> These command line options are accumulative from left to right, so if you do:
>
> --cplusplus_polymorphic_class=BufferedOstream
> --cplusplus_polymorphic_class=HyperOstream
>
> then it's the same as doing:
>
> --cplusplus_polymorphic_class=BufferedOstream,HyperOstream
>
> However if you use "+" to turn it on fully, or if you use "-" to turn
> it off fully, then these two options wipe out the previous one's. So
> for example if you do:
>
> my_favourite_compiler --cplusplus_polymorphic_class=+
> --cplusplus_polymorphic_class=BufferedOstream
> --cplusplus_polymorphic_class=-
>
> Then it's the same as doing:
>
> my_favourite_compiler --cplusplus_polymorphic_class=-
>
> So maybe we should have compiler options in future C++ standards to
> achieve higher levels of versatility.
>
> By the way, C++ programmers writing code today in 2022 have to use
> some compiler options to get their standard-compliant code to compile.
> For example if I want to use "std::thread" in the GNU compiler (g++)
> then I have to compile with "-pthread".
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2022-07-31 10:40:20