C++ Logo

sg7

Advanced search

Re: [SG7] Metaprogramming

From: Ville Voutilainen <ville.voutilainen_at_[hidden]>
Date: Mon, 26 Oct 2020 22:15:18 +0200
On Mon, 26 Oct 2020 at 22:08, David Rector <davrec_at_[hidden]> wrote:
> Here is Andrew’s proposed syntax:
>
> ```
> template<typename T>
> consteval meta::info property(string_view id) {
> string member_name = “m_” + id;
> string getter_name = “get_” + id;
> string setter_name = “set_” + id;
> return <class {
> private:
> T |# %{member_name} #|;
> public:
> T const& |# %{getter_name} #|() const {
> return |# %{member_name} #|;
> }
> void |# %{setter_name} #|(T const& x) {
> |# %{member_name} #| = x;
> }
> }>;
> }
>
> struct book {
> << property<string>(“author”);
> << property<string>(“title”);
> // other book properties
> };
> ```
>
> Here is one alternative, *which would use Andrew’s very same semantics*, but simply changes the syntax to resemble an output stream users may be more familiar with. To me, this stands out at least as much, if not more (imagine all the string literals below are colored red in your IDE), yet remains familiar.
>
> ```
>
> template<typename T>
> consteval void inject_property(string_view id) {
> string member_name = “m_” + id;
> string getter_name = “get_” + id;
> string setter_name = “set_” + id;
>
>
>
> meta << "private:"
> " T " << member_name
> << "public: "
> "T const& " << getter_name << "() const {"
> " return " << member_name
> << "}"
> "void " << setter_name << "(T const& x) {"
> << member_name << " = x;"
> "}";
> }
>
> struct book {
> consteval {
> inject_property<string>(“author”);
> inject_property<string>(“title”);
> // other book properties
> }
> };
> ```
>
> Plenty of tweaks possible as well, all so easy to implement/try out.
>
> The nice thing about enclosing meta content in quotes in this way is that initially, you can only allow certain "hygienic" formulations, giving errors wherever the user doesn’t follow them — but later on, you can ease the burdens, and permit string injection of full declarations and statements as I have shown it is possible to implement, should users identify specific examples that absolutely require it. It seems the perfect compromise.
>
> I hope this committee will give this and other easy to implement syntactic alternatives serious consideration.

When I read those two examples, my brain suggests to me that I'm
reading a class definition when reading the first,
and doesn't suggest much anything when I'm reading the second, except
that there's chained streaming operators
in it.

The committee should give serious consideration for proposals with
rationale. The key part of that is the rationale.

Received on 2020-10-26 15:15:32