C++ Logo

STD-PROPOSALS

Advanced search

Subject: [std-proposals] Base class name aliases
From: Daniel Milyutin (DMilyutin_at_[hidden])
Date: 2019-11-20 08:47:31


Greetings to all readers,

My name is Daniel.

My proposal is on reducing boilerplate when inheriting and having alias of baseclass.

Quite common situation is so far.
You inherit from (templated) class and want to have some aliases for base class names.
Like so:
///code starts
template<typename T1, typename T2, typename T3>
class Derived: public Base<T1, T2>, protected Policy<T3>
{
protected:
  using help = Policy<T3>;
public:
  using super = Base<T1, T2>;
  void foo()
  {
    super::foo(help::someMethod());//
  }
};
///code ends
Non-templated case may be seen in the same fashion.
Templated version is more typing and more errors to do.

In example above one have to type-in (copy-paste - to be honest) names of base classes.
When base class changes you have to type its name twice and actually you may miss update for alias.

To reduce this boilerplate let us consider the following example of syntax:
/// code starts
template<typename T1, typename T2, typename T3>
class Derived: public Base<T1, T2> -> super, protected Policy<T3> -> help
{
public:
  void foo()
  {
    super::foo(help::someMethod());//ex.
  }
};
/// code ends
Herein let it be assumed that alias is with same access as in inherited classes
(i.e. super is public and help is protected).
Symbol "->" is just for sake of example. Think of this as some syntax allowing to have this functionality.
More options for syntax will be described a little further.
To control access to alias from derived class one may have this syntax:
/// code starts
template<typename T1, typename T2, typename T3>
class Derived: public Base<T1, T2> ->private super, protected Policy<T3> -> public help
...
/// code ends
Note "private super" and "public help".

The rule options for default behavior when access is not specified may be:
(in order of my current preference):
RO1) when there is no [opt-access] use access same as of base class
RO2) when there is no [opt-access] use alias access as private
RO3) when there is no [opt-access] use alias access as public

Syntax options are(to my estimation of most likeliness):
SO1) public Base<T1, T2> -> [opt-access] super
SO2) public Base<T1, T2> = [opt-access] super
SO3) [opt-access] using super = public Base<T1, T2>
SO4) [opt-access] super = public Base<T1, T2>
SO5) public Base<T1, T2> typedef [opt-access] super
SO6) public Base<T1, T2> [opt-access] typedef super
SO5) public Base<T1, T2> using [opt-access] super
SO6) public Base<T1, T2> [opt-access] using super
SO7) public Base<T1, T2>([opt-access] super)
SO8) public Base<T1, T2> as [opt-access] super
SO9) public Base<T1, T2> => [opt-access] super

There may be more. I'd prefer less verbose 1).
But also I'd like to distinguish from "->" operator. So I actually wrote "=>" operator.
I'm aware there are no operators "=>" or "as" in C++.and probably won't be.
That's it.
_________

That is my first proposal.
I don't know how further process of consideration and discussion goes.
And I hope I send this to right e-mail.

Best Regards,
Daniel.
Confidentiality Notice: This message (including attachments) is a private communication solely for use of the intended recipient(s). If you are not the intended recipient(s) or believe you received this message in error, notify the sender immediately and then delete this message. Any other use, retention, dissemination or copying is prohibited and may be a violation of law, including the Electronic Communication Privacy Act of 1986.



STD-PROPOSALS list run by herb.sutter at gmail.com

Standard Proposals Archives on Google Groups