C++ Logo

std-proposals

Advanced search

Re: Proposal to simplify creating constructors

From: Oleksii Tarasiuk <o.tarasiuk_at_[hidden]>
Date: Fri, 7 Feb 2020 09:54:23 +0100
Re: [std-proposals] Proposal to simplify creating constructors Hi Михаил.

Thank you very much for your prompt reply, which inspires me to write further proposals.
Yes, I agree that implementing that in general case may be too difficult.
But how about implementing it just for the case of inline constructors?
In fact, this case happens very often. For example, every constructor of every template class/struct is inline.

In addition, please consider the following even more reduced form:
// [Case 3]
class Person {
private:
  string name_;
  Date birthDate_;
  //.... other data members.
public:
  Person(.name_, .birthDate_, SomeOtherType someOtherArg)
  {
    // use someOtherArg to further init the instance of Person.
  }
};

The [Case 3] may be treated as equivalent of the following:
// [Case 4]
class Person {
private:
  string name_;
  Date birthDate_;
  //.... other data members.
public:
  Person(string&& a, Date&& b, SomeOtherType someOtherArg)
    : name_(std::forward<A>(a))
    , birthDate_(std::forward<B>(b))
  {
    // use someOtherArg to further init the instance of Person.
  }
  Person(string&& a, Date const& b, SomeOtherType someOtherArg)
    : name_(std::forward<A>(a))
    , birthDate_(b)
  {
    // use someOtherArg to further init the instance of Person.
  }
  Person(string const& a, Date&& b, SomeOtherType someOtherArg)
    : name_(a)
    , birthDate_(std::forward<B>(b))
  {
    // use someOtherArg to further init the instance of Person.
  }
  Person(string const& a, Date const& b, SomeOtherType someOtherArg)
    : name_(a)
    , birthDate_(b)
  {
    // use someOtherArg to further init the instance of Person.
  }
};

Friday, February 7, 2020, 8:57:04 AM you wrote:





On Thu, Feb 6, 2020 at 9:17 PM Oleksii Tarasiuk via Std-Proposals <std-proposals_at_[hidden]> wrote:

Hi all!

Let's assume we have the following class:
// [Case 1]
class Person {
private:
  string name_;
  Date birthDate_;
  //.... other data members.
public:
  Person(string&& name, Date const& birthDate, SomeOtherType someOtherArg)
    : name_(forward<string>(name), birthDate_(birthDate)
  {
    // use someOtherArg to further init the instance of Person.
  }
};

I propose to add support for the following simplified form which is equivalent to [Case 1]:

// [Case 2]
class Person {
private:
  string name_;
  Date birthDate_;
  //.... other data members.
public:
  Person(auto&& .name_, auto const& .birthDate_, SomeOtherType someOtherArg)
  {
    // use someOtherArg to further init the instance of Person.
  }
};

By this model the constructor must be inline which is often not possible/recommended/desirable.
If it is not inline, things begin to become tricky, as the declaration will still have to name the members (so the compiler knows for which definition to look for)
Members being part of the ctor declaration is ... questionable as it is no longer just an interface.

The manual duplication is definitely annoying, but I am afraid a bunch of new corner rules and new behavior will skew the scales to "not worth the effort" just enough.






--
Best regards,
Oleksii Tarasiuk                          mailto:
o.tarasiuk_at_[hidden]

--
Std-Proposals mailing list
Std-Proposals_at_[hidden]
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals



--
Best Regards,
Oleksii Tarasiuk                        
mailto:o.tarasiuk_at_[hidden]

Received on 2020-02-07 02:57:07